How to check file exist using c language

A

andylcx

Hi all:
The c++ language can check whether the file exist or not. I am
wondering how c language does this job? Thanks a lot!

Andy
 
G

Gordon Burditt

Hi all:
The c++ language can check whether the file exist or not. I am
wondering how c language does this job? Thanks a lot!

fopen()

There is no absolutely correct way to check whether a file exists
or not, since the security rules of the OS may not permit you
to know whether the file exists or not. But fopen() in "r"
mode gives you a definite YES or a probable NO.

Gordon L. Burditt
 
K

Keith Thompson

fopen()

There is no absolutely correct way to check whether a file exists
or not, since the security rules of the OS may not permit you
to know whether the file exists or not. But fopen() in "r"
mode gives you a definite YES or a probable NO.

On the other hand, the information may not be particularly useful.
You can write a function that attempts to fopen() a file, records
whether it succeeded, fclose()s the file, and returns the result of
the fopen(). The file may no longer exist by the time you try to use
it. For that matter, an fopen() call has to specify whether the file
is to be opened in text or binary mode. If the OS makes a strong
distinction between text and binary files, it may be that one will
succeed and the other will fail. And, of course, knowing that a file
exists doesn't guarantee that you can do anything with it.

Presumably you want to know whether a file exists so you can find out
whether you can do something with it. Often a better approach is to
go ahead and try to do what you wanted to do in the first place, and
handle any errors that result.
 
M

Malcolm

Gordon Burditt said:
There is no absolutely correct way to check whether a file exists
or not, since the security rules of the OS may not permit you
to know whether the file exists or not. But fopen() in "r"
mode gives you a definite YES or a probable NO.
However if you close the file, and then try to open it again in the next
statement, it may have disappeared.
 
J

Jack Klein

Hi all:
The c++ language can check whether the file exist or not.

No, it cannot. And neither can the C++ standard library, which is
much more pertinent. The person who told you this is mistaken. Even
if you yourself are that person.
I am
wondering how c language does this job? Thanks a lot!

C does not do this job, neither the language nor the standard library.
 
K

Keith Thompson

Is there any example availabe for my reference? Thanks a lot

Please don't top-post. I've corrected it here.

What kind of example do you have in mind?

If a program opens a file, then closes it, then opens the same file
again, there must be some finite time between closing it and
re-opening it. Another program running on the same system could
delete the file during that gap. (This assumes a system on which
multiple programs can execute simultaneously.)
 
M

Malcolm

Keith Thompson said:
If a program opens a file, then closes it, then opens the same file
again, there must be some finite time between closing it and
re-opening it. Another program running on the same system could
delete the file during that gap. (This assumes a system on which
multiple programs can execute simultaneously.)
Or a system with a floppy. If the user presses the eject button, you might
just get unlucky. Probably not worth bothering about for a video game, but
if the program is dealing with medical data it wouldn't be acceptable.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,835
Latest member
KetoRushACVBuy

Latest Threads

Top