By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,760 Members | 1,200 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,760 IT Pros & Developers. It's quick & easy.

How to check file exist using c language

P: n/a
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

Nov 15 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
>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
Nov 15 '05 #2

P: n/a
go****@hammy.burditt.org (Gordon Burditt) writes:
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.


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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <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.
Nov 15 '05 #3

P: n/a

"Gordon Burditt" <go****@hammy.burditt.org> wrote
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.
Nov 15 '05 #4

P: n/a
Is there any example availabe for my reference? Thanks a lot

Malcolm wrote:
"Gordon Burditt" <go****@hammy.burditt.org> wrote
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.


Nov 15 '05 #5

P: n/a
On 6 Jul 2005 13:55:38 -0700, an*****@hotmail.com wrote in
comp.lang.c:
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.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 15 '05 #6

P: n/a
an*****@hotmail.com writes:
Malcolm wrote:
"Gordon Burditt" <go****@hammy.burditt.org> wrote
> 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.


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.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <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.
Nov 15 '05 #7

P: n/a

"Keith Thompson" <ks***@mib.org> wrote
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.
Nov 15 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.