Good way of checking if file exists ?

R

Robert Frunzke

a standard C compatible way could be:

FILE *fp = fopen("file","r");
if( fp ) {
// exists
fclose(fp);
} else {
// doesnt exist
}
 
J

Joona I Palaste

Robert Frunzke said:
a standard C compatible way could be:
FILE *fp = fopen("file","r");
if( fp ) {
// exists
fclose(fp);
} else {
// doesnt exist

Doesn't exist, exists but isn't readable, exists but already has too
many locks, or other similar reasons.

It's impossible to check existence for certain in pure ISO standard C.

--
/-- Joona Palaste ([email protected]) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"Parthenogenetic procreation in humans will result in the founding of a new
religion."
- John Nordberg
 
G

Geiregat Jonas

is using
if(open("file",O_EXCL) != -1){ printf("File does
exists")}else{printf("file does not exists"); }

a good way of checking if a file exists or not, if not how should I do it ?
 
E

Eric

Geiregat Jonas said:
is using
if(open("file",O_EXCL) != -1){ printf("File does
exists")}else{printf("file does not exists"); }

a good way of checking if a file exists or not, if not how should I do it ?

I would recommend using 'fopen' instead to use a standard c function.

However, standard C doesn't provide any guaranteed way to determine
whether a file exists or not. fopen can fail even if a file does exist.

However, it is possible that it is a reasonable method for your system,
but that question could only be answered by people knowledgeable with
your specific system.
 
C

Coos Haak

Op Wed, 07 Jan 2004 22:35:17 +0000 schreef Geiregat Jonas
Using the stat function would solve some probleme's because you don't
need any access rights. I think stat is the best way (I'm using linux).

access rights and stat are unknown to pure ISO standard C.
 
K

Keith Thompson

Geiregat Jonas said:
is using
if(open("file",O_EXCL) != -1){ printf("File does
exists")}else{printf("file does not exists"); }

a good way of checking if a file exists or not, if not how should I do it ?

There's no really good portable way to determine whether a named file
exists; you'll probably have to resort to system-specific methods
(which you can ask about in another newsgroup).

Before you do, you should define more clearly what information you're
looking for and what you intend to do with it. On a multi-processing
system, for example, if your logic is something like this:

if (file exists) {
open existing file
}
else {
create new file
initialize new file
}

you should allow for the possibility that the file is created <OT>by
another process</OT> after you check for its existence and before you
attempt to create it.

Note also that if a file doesn't exist, that doesn't imply that you
have permission to create it.

In many cases, it may not be possible to determine whether a file
exists. On a system with user accounts and directories with
permission settings, an attempt to determine whether a file exists may
fail because you don't have permission to read the directory that may
or may not contain it.
 
G

Geiregat Jonas

Joona said:
Doesn't exist, exists but isn't readable, exists but already has too
many locks, or other similar reasons.




It's impossible to check existence for certain in pure ISO standard C.
Using the stat function would solve some probleme's because you don't
need any access rights. I think stat is the best way (I'm using linux).
 
R

Rahul Agarkar

Geiregat Jonas said:
is using
if(open("file",O_EXCL) != -1){ printf("File does
exists")}else{printf("file does not exists"); }

a good way of checking if a file exists or not, if not how should I do it ?

In my opinion, the best option is to use access() call instead of
open() call. In this case, the above given code will look like:

if (access("file", F_OK) == 0)
{
printf("Files does exists");
}
else
{
printf("File does not exist");
...
}

Regards,
- Rahul Agarkar
 
J

Joona I Palaste

In my opinion, the best option is to use access() call instead of
open() call. In this case, the above given code will look like:
if (access("file", F_OK) == 0)
{
printf("Files does exists");
}
else
{
printf("File does not exist");
...
}

Neither access() or open() are ISO standard C functions. Whatever next,
will you be suggesting OpenFile() in <windows.h> or something?
(That function name might be wrong. I haven't actually *looked* at
<windows.h>.)
 
C

CBFalconer

Rahul said:
In my opinion, the best option is to use access() call instead of
open() call. In this case, the above given code will look like:

if (access("file", F_OK) == 0)
printf("Files does exists");
else {
printf("File does not exist");
...
}

(slightly reformatted). open, access, O_EXCL and F_OK do not
exist in standard C, the subject of this newsgroup. Please do not
give off-topic answers here, where there may well be nobody to
correct any errors.
 
G

Geiregat Jonas

CBFalconer said:
(slightly reformatted). open, access, O_EXCL and F_OK do not
exist in standard C, the subject of this newsgroup. Please do not
give off-topic answers here, where there may well be nobody to
correct any errors.

Where could I find information what is Standard ISO C
and information about other standards ?
I'm using stat now.
 
D

Default User

Geiregat said:
Where could I find information what is Standard ISO C
and information about other standards ?
I'm using stat now.

The obvious answer is, the ISO C Standard.




Brian Rodenborn
 
G

Geiregat Jonas

Default said:
Geiregat Jonas wrote:




The obvious answer is, the ISO C Standard.




Brian Rodenborn

I think it was obvious that I knew that but was looking for the official
place to get it from.
 

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

Similar Threads


Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top