Re: File exist



Mr John FO Evans <mijas@xxxxxxxxxxxxxxxxx> writes:
In article <76reh3xu85.ln2@xxxxxxxxxxxxxxxxxxxxxxx>, Flash Gordon
<spam@xxxxxxxxxxxxxxxxxx> wrote:
[...]
It is not part of standard C. As I'm sure others have said in this
thread standard C does not provide any portable method to see if a file
exists. So you can be sure that any method you find goes beyond what
standard C provides.

What is wrong with attempting to open the file and if there is no error
closing it again? Surely this is standard 'C'?

Yes, but it only tells you whether you were able to open the file; it
doesn't necessarily tell you why. (And fopen() doesn't even
necessarily set errno to a meaningful value.)

On some systems, it might be possible to open a file in binary mode
but not in text mode, or vice versa.

In some cases, it's not even possible to determine whether a file
exists; you might not have permission to ask (e.g., if the file is in
a directory you don't have permission to read). Also, a file might
exist when you test it, but cease to exist before you use it.

Usually the best approach is to try to open the file, and handle the
error if the attempt fails. Providing information about *why* it
failed can be useful, but it isn't absolutely necessary, and it can't
be done portably.

--
Keith Thompson (The_Other_Keith) kst-u@xxxxxxx <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
.



Relevant Pages

  • Re: WHY doesnt C know anything about directories?
    ... does the C standard have no abstract and portable method for ... dealing with directories? ... On embedded systems you can just return empty lists. ...
    (comp.lang.c)
  • Re: WHY doesnt C know anything about directories?
    ... does the C standard have no abstract and portable method for ... dealing with directories? ...
    (comp.lang.c)
  • Re: WHY doesnt C know anything about directories?
    ... does the C standard have no abstract and portable method for ... dealing with directories? ...
    (comp.lang.c)
  • Re: File exist
    ... There is no statfunction in standard C. Using it will limit the ... portability of your code. ... int access ... As I'm sure others have said in this thread standard C does not provide any portable method to see if a file exists. ...
    (comp.lang.c)
  • Re: File exist
    ... There is no statfunction in standard C. Using it will limit the ... portability of your code. ... int access ... As I'm sure others have said in this thread standard C does not provide any portable method to see if a file exists. ...
    (comp.lang.c)