Page 1 of 1

fopen error codes on failure

PostPosted: Aug 18, 2005 @ 1:48am
by Dan East
I try to use the fopen API for file IO as much as possible for portability. Typically if a file can't be opened I don't need to know why. However in some cases, for example when multiple programs read and write to the same file, I need to know if the file is simply locked for reading or writing, as opposed to it failing for some other reason (like the path is invalid, or the drive is full, etc).

With CreateFile and GetLastError I can determine exactly why I couldn't open the file.

Is there such a method with fopen? Or do I have to use a non-portable API to get that extra info?

Dan East

PostPosted: Aug 18, 2005 @ 4:09am
by Andy
strerror(errno)

According to KR: will give "an implementation defined error message corresponding to the integer in errno".

PostPosted: Aug 18, 2005 @ 4:32am
by Dan East
Yeah, it looks like errno==EACCES will do it.

If not, I can always do a FindFirstFile to see if the file exists, and make sure it isn't a directory. If it meets that criteria but fopen can't open it then I can assume it is locked.

Thanks!

Dan East