Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to get Win32 error code related to fopen?

470 views
Skip to first unread message

Olav Tollefsen

unread,
Jan 5, 1995, 6:14:49 AM1/5/95
to
If my "fopen" call fails, how can I get the Win32 error code which
tells me exactly what went wrong?
--
Olav Tollefsen
Digital Equipment Corporation, Norway
233 MHz Alpha AXP + Windows NT 3.5 = RAW SPEEED!!!

Jim Patterson

unread,
Jan 5, 1995, 10:29:12 AM1/5/95
to
Olav Tollefsen (olav.to...@nwo.mts.dec.com) wrote:
: If my "fopen" call fails, how can I get the Win32 error code which

: tells me exactly what went wrong?

GetLastError() will work, provided there hasn't been another failed call
to mess up the error status, and provided the open failure was in fact
caused by a WIN32 API failure and not an invalid parameter or
something.

A more reliable way is to open the file yourself using CreateFile; you
can then get a corresponding FILE* by doing an
fdopen(_open_osfhandle(hdl,mde)).

--
Jim Patterson Cognos Incorporated
Sr Consulting Engineer P.O. BOX 9707
UUNET:ji...@cognos.COM 3755 Riverside Drive
PHONE:(613)738-1338 x3385 Ottawa, Ont K1G 3Z4

Olav Tollefsen

unread,
Jan 6, 1995, 4:38:48 AM1/6/95
to
In article <1995Jan5.1...@cognos.com>
ji...@cognos.COM (Jim Patterson) wrote:

> Olav Tollefsen (olav.to...@nwo.mts.dec.com) wrote:
> : If my "fopen" call fails, how can I get the Win32 error code which
> : tells me exactly what went wrong?
>
> GetLastError() will work, provided there hasn't been another failed call
> to mess up the error status, and provided the open failure was in fact
> caused by a WIN32 API failure and not an invalid parameter or
> something.

If I have some code like this:

FILE* file = fopen("a.a", "w");
if (file == NULL)
{
DWORD nStatus = GetLastError();
...

In the above case, the status returned will be zero (NO_ERROR).

Thomas Nimstad

unread,
Jan 6, 1995, 8:27:00 AM1/6/95
to
olav.to...@nwo.mts.dec.com (Olav Tollefsen) writes:
>If I have some code like this:
>
>FILE* file = fopen("a.a", "w");
>if (file == NULL)
>{
> DWORD nStatus = GetLastError();
> ...
>
>In the above case, the status returned will be zero (NO_ERROR).

You'll have to use the Win32 CreateFile() to use GetLastError(). The
use of old, none thread-safe functions in the rtl is out. Pure Win32
calls are in.

// JTN (j...@jd.se)

---
* Thomas Nimstad * #pragma message("Standard disclaimer in use")
* Juristdata AB, SWEDEN * Professional development for Windows NT
* Phone +46-500 412150 * Fax +46-500 412848

Van Chinh Ly

unread,
Jan 6, 1995, 9:54:14 PM1/6/95
to
In article <3ej338$6...@nntpd.lkg.dec.com>, olav.to...@nwo.mts.dec.com (Olav Tollefsen) says:
>If I have some code like this:
>
>FILE* file = fopen("a.a", "w");
>if (file == NULL)
>{
> DWORD nStatus = GetLastError();
> ...
>
>In the above case, the status returned will be zero (NO_ERROR).
>--

My guess is that the run-time code for fopen() makes several
Win32 API calls. One of them (not the last one) does the
actual CreatFile() call. However, the API calls after that failed
call were successful. Therefore, GetLastError() returns NO_ERROR.

My suggestion would be to be consistent (i.e. either use CreateFile()
and then use GetLastError() or use fopen() and then use ferror() or
something).

Van Ly

Steve Salisbury

unread,
Jan 11, 1995, 4:18:15 PM1/11/95
to
In article <010695162...@jd.se> j...@jd.se (Thomas Nimstad) writes:
>olav.to...@nwo.mts.dec.com (Olav Tollefsen) writes:
>>If I have some code like this:
>>
>>FILE* file = fopen("a.a", "w");
>>if (file == NULL)
>>{
>> DWORD nStatus = GetLastError();
>> ...
>>
>>In the above case, the status returned will be zero (NO_ERROR).
>
>You'll have to use the Win32 CreateFile() to use GetLastError(). The
>use of old, none thread-safe functions in the rtl is out. Pure Win32
>calls are in.

If you are using Microsoft C/C++, the error code from the operating system
is stored in the global variable "_doserrno". If you link with multi-
thread safe libraries (LIBCMT.LIB or MSVCRT.LIB), _doserrno is #define-d
into a function since this value is per-thread. (This is also true of
errno, and the ANSI C documents allow errno to be turned into a function
call returning a pointer to an integer -- it just has to be an "lvalue",
something that can go on the left hand side of an assignment statement.)

Calling CreateFile is not at all comparable to fopen since the latter
gives you buffering.

This is not official technical support but rather a personal attempt to
be helpful.

Thanks,
Steve Salisbury

# Obligatory_ # |-------------------------------------------------------|
# disclaimer_ # | The views expressed in this message are my own and in |
# required by # | no way reflect the views of Microsoft Corporation. |
# my employer # |-------------------------------------------------------|

0 new messages