Connecting Tech Pros Worldwide Forums | Help | Site Map

Good way of checking if file exists ?

Geiregat Jonas
Guest
 
Posts: n/a
#1: Nov 14 '05
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 ?

Robert Frunzke
Guest
 
Posts: n/a
#2: Nov 14 '05

re: Good way of checking if file exists ?


a standard C compatible way could be:

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




Geiregat Jonas wrote:[color=blue]
> 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 ?
>[/color]
Joona I Palaste
Guest
 
Posts: n/a
#3: Nov 14 '05

re: Good way of checking if file exists ?


Robert Frunzke <Robert.no.spam.F@freenet.de> scribbled the following:[color=blue]
> a standard C compatible way could be:[/color]
[color=blue]
> FILE *fp = fopen("file","r");
> if( fp ) {
> // exists
> fclose(fp);
> } else {
> // doesnt exist[/color]

Doesn't exist, exists but isn't readable, exists but already has too
many locks, or other similar reasons.
[color=blue]
> }[/color]

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

--
/-- Joona Palaste (palaste@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"Parthenogenetic procreation in humans will result in the founding of a new
religion."
- John Nordberg
Eric
Guest
 
Posts: n/a
#4: Nov 14 '05

re: Good way of checking if file exists ?


Geiregat Jonas <eniac@sdf-eu.org> wrote:
[color=blue]
> 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 ?[/color]

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.

--
== Eric Gorr ========= http://www.ericgorr.net ========= ICQ:9293199 ===
"Therefore the considerations of the intelligent always include both
benefit and harm." - Sun Tzu
== Insults, like violence, are the last refuge of the incompetent... ===
Geiregat Jonas
Guest
 
Posts: n/a
#5: Nov 14 '05

re: Good way of checking if file exists ?


Joona I Palaste wrote:[color=blue]
> Robert Frunzke <Robert.no.spam.F@freenet.de> scribbled the following:
>[color=green]
>>a standard C compatible way could be:[/color]
>
>[color=green]
>>FILE *fp = fopen("file","r");
>>if( fp ) {
>> // exists
>> fclose(fp);
>>} else {
>> // doesnt exist[/color]
>
>
> Doesn't exist, exists but isn't readable, exists but already has too
> many locks, or other similar reasons.
>
>[color=green]
>>}[/color]
>
>
> It's impossible to check existence for certain in pure ISO standard C.
>[/color]
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).

Coos Haak
Guest
 
Posts: n/a
#6: Nov 14 '05

re: Good way of checking if file exists ?


Op Wed, 07 Jan 2004 22:35:17 +0000 schreef Geiregat Jonas
<eniac@sdf-eu.org>:
[color=blue]
>Joona I Palaste wrote:[color=green]
>> Robert Frunzke <Robert.no.spam.F@freenet.de> scribbled the following:
>>[color=darkred]
>>>a standard C compatible way could be:[/color]
>>
>>[color=darkred]
>>>FILE *fp = fopen("file","r");
>>>if( fp ) {
>>> // exists
>>> fclose(fp);
>>>} else {
>>> // doesnt exist[/color]
>>
>>
>> Doesn't exist, exists but isn't readable, exists but already has too
>> many locks, or other similar reasons.
>>
>>[color=darkred]
>>>}[/color]
>>
>>
>> It's impossible to check existence for certain in pure ISO standard C.
>>[/color]
>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).[/color]

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

--
Coos
Keith Thompson
Guest
 
Posts: n/a
#7: Nov 14 '05

re: Good way of checking if file exists ?


Geiregat Jonas <eniac@sdf-eu.org> writes:[color=blue]
> 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 ?[/color]

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.

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
Rahul Agarkar
Guest
 
Posts: n/a
#8: Nov 14 '05

re: Good way of checking if file exists ?


Geiregat Jonas <eniac@sdf-eu.org> wrote in message news:<3ffc2a96$0$1805$ba620e4c@news.skynet.be>...[color=blue]
> 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 ?[/color]

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
Joona I Palaste
Guest
 
Posts: n/a
#9: Nov 14 '05

re: Good way of checking if file exists ?


Rahul Agarkar <rahul_agarkar@hotmail.com> scribbled the following:[color=blue]
> Geiregat Jonas <eniac@sdf-eu.org> wrote in message news:<3ffc2a96$0$1805$ba620e4c@news.skynet.be>...[color=green]
>> 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 ?[/color][/color]
[color=blue]
> 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:[/color]
[color=blue]
> if (access("file", F_OK) == 0)
> {
> printf("Files does exists");
> }
> else
> {
> printf("File does not exist");
> ...
> }[/color]

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

--
/-- Joona Palaste (palaste@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"This isn't right. This isn't even wrong."
- Wolfgang Pauli
CBFalconer
Guest
 
Posts: n/a
#10: Nov 14 '05

re: Good way of checking if file exists ?


Rahul Agarkar wrote:[color=blue]
> Geiregat Jonas <eniac@sdf-eu.org> wrote in message
>[color=green]
> > 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 ?[/color]
>
> 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");
> ...
> }[/color]

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

--
Chuck F (cbfalconer@yahoo.com) (cbfalconer@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!


Geiregat Jonas
Guest
 
Posts: n/a
#11: Nov 14 '05

re: Good way of checking if file exists ?


CBFalconer wrote:[color=blue]
> Rahul Agarkar wrote:
>[color=green]
>>Geiregat Jonas <eniac@sdf-eu.org> wrote in message
>>
>>[color=darkred]
>>>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 ?[/color]
>>
>>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");
>> ...
>>}[/color]
>
>
> (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.
>[/color]

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

CBFalconer
Guest
 
Posts: n/a
#12: Nov 14 '05

re: Good way of checking if file exists ?


Geiregat Jonas wrote:[color=blue]
> CBFalconer wrote:[color=green]
> > Rahul Agarkar wrote:
> >[color=darkred]
> >>Geiregat Jonas <eniac@sdf-eu.org> wrote in message
> >>
> >>
> >>>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");
> >> ...
> >>}[/color]
> >
> > (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.[/color]
>
> Where could I find information what is Standard ISO C
> and information about other standards ?
> I'm using stat now.[/color]

Try:
<http://www.dinkumware.com>
<http://std.dkuug.dk/jtc1/sc22/wg14/www/docs/n869/>

for the latter I advise downloading the text version for easy
searching, grepping, etc.

--
Chuck F (cbfalconer@yahoo.com) (cbfalconer@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!


Default User
Guest
 
Posts: n/a
#13: Nov 14 '05

re: Good way of checking if file exists ?


Geiregat Jonas wrote:
[color=blue]
> Where could I find information what is Standard ISO C
> and information about other standards ?
> I'm using stat now.[/color]

The obvious answer is, the ISO C Standard.




Brian Rodenborn
Geiregat Jonas
Guest
 
Posts: n/a
#14: Nov 14 '05

re: Good way of checking if file exists ?


Default User wrote:[color=blue]
> Geiregat Jonas wrote:
>
>[color=green]
>>Where could I find information what is Standard ISO C
>>and information about other standards ?
>>I'm using stat now.[/color]
>
>
> The obvious answer is, the ISO C Standard.
>
>
>
>
> Brian Rodenborn[/color]

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

Eric
Guest
 
Posts: n/a
#15: Nov 14 '05

re: Good way of checking if file exists ?


Geiregat Jonas <eniac@sdf-eu.org> wrote:
[color=blue]
> I think it was obvious that I knew that but was looking for the official
> place to get it from.[/color]

I believe this is it:

Search for:

9899:1999

at

http://www.iso.ch

which should bring up this page:

http://tinyurl.com/3bpmj

which is what I believe you are looking for.


--
== Eric Gorr ========= http://www.ericgorr.net ========= ICQ:9293199 ===
"Therefore the considerations of the intelligent always include both
benefit and harm." - Sun Tzu
== Insults, like violence, are the last refuge of the incompetent... ===
Kevin Goodsell
Guest
 
Posts: n/a
#16: Nov 14 '05

re: Good way of checking if file exists ?


Eric wrote:[color=blue]
> Geiregat Jonas <eniac@sdf-eu.org> wrote:
>
>[color=green]
>>I think it was obvious that I knew that but was looking for the official
>>place to get it from.[/color]
>
>
> I believe this is it:
>
> Search for:
>
> 9899:1999
>
> at
>
> http://www.iso.ch
>
> which should bring up this page:
>
> http://tinyurl.com/3bpmj
>
> which is what I believe you are looking for.
>
>[/color]

It should also be available from various national standards bodies, such
as ANSI in the US (which sells the downloadable PDF for $18, last I heard).

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
Closed Thread