Reputation: 9679
I'd like to perform a quick check whether or not a file can be opened. It should be written in portable C, or at least to work on Win32 and POSIX systems. #ifdefs are acceptable.
I'm trying to avoid this:
int openable(const char*filename) {
FILE *f = fopen(filename,"r");
if (!f)
return 0; /* openable */
fclose(f);
return 1; /* not openable */
}
From what I can tell stat(), in its simplest form, can be used to check if file exists, but not to check if it's actually openable.
Upvotes: 2
Views: 1886
Reputation: 399881
The POSIX standard solution is access()
, which is also in the Windows runtime as _access()
.
I guess it is slighly better than fopen()
+fclose()
because:
Of course, it is just as susceptible to race conditions as any other way of doing such a test. In a way, the only safe way to know if a file is availble for reading is to open it and try to read, without closing it in between. Even then, you still need to watch for "unexpected" EOF, of course. I/O is hard.
Upvotes: 3
Reputation: 340218
I think what you have is probably about as good as you'll get (though your comments are reversed from what they should be). There are many things that can prevent a file from opening: permissions, something else holding it open, lack of resources, etc.
To go through all of these checks accurately, you might as well just open the thing.
However, note also that on most systems, what you return could be a lie to the caller - by the time you've returned an indication to the caller about whether the file can be opened or not, the state of the systems could change (something that held the file open exclusively could close it for example). So the only truly effective way for an application to know whether they can open and use a file is to just open and use the file (and handle any failures).
In other words, if you return 'true
' to your caller, they could try to open the file and it might still fail.
Upvotes: 1
Reputation: 7924
There are many factors you would need to check before you can know a file is "openable", and trying to check them all in a cross-platform way would be silly.
I think what you are doing now is simple and safer. However remember that just because a file is "openable" now doesn't mean that it still will be when you actually come to open it. Nothing prevents it changing in the meantime.
Upvotes: 1