How to check file exists.

R

Raymond

Hi All:

To find a file exists using the file name, I have tow routings on UNIX
system.

1. access(2)
2. lstat(2)

This tow function also can do. When the return value is "-1" and errno
is "ENOENT", The file doesn't exist.

**Which one is better?

Thank you very much.
Raymond Shen.
 
J

Jack Klein

Hi All:

To find a file exists using the file name, I have tow routings on UNIX
system.

1. access(2)
2. lstat(2)

This tow function also can do. When the return value is "-1" and errno
is "ENOENT", The file doesn't exist.

**Which one is better?

Thank you very much.
Raymond Shen.

Neither, because neither of them is part of standard C, so they are
off-topic here. If you want a comparison of non-standard extensions
provided by UNIX, ask in
 
G

Gordon Burditt

To find a file exists using the file name, I have tow routings on UNIX
system.

1. access(2)
2. lstat(2)

This tow function also can do. When the return value is "-1" and errno
is "ENOENT", The file doesn't exist.

**Which one is better?

fopen() has the advantage of existing in standard C, and if
it succeeds, the file exists and you can access it.

All 3 functions have different ways in which they can give an
indecisive answer:

- lstat() can give a false positive, depending on what you think about
the situation, if the name refers to a broken symlink.
- access() can give a false positive or negative due to permissions
issues if the effective and real user ids are different.
- All 3 can fail with errno == EPERM and you don't know whether the
file exists or not, because you don't have permission to know that.
- fopen() fails on files which exist but you don't have permission
to use the mode (read or write) which you asked for.

Gordon L. Burditt
 
R

Raymond

Gordon said:
fopen() has the advantage of existing in standard C, and if
it succeeds, the file exists and you can access it.

Thank you Gordon.
But I only want to know whether the file exists. If fopen success, The
file is open and a system file handler is assigned. I must close the
handler. Consider the performance of the application. I'd like use a
lighter routing.
 
A

Alan Balmer

Thank you Gordon.
But I only want to know whether the file exists. If fopen success, The
file is open and a system file handler is assigned. I must close the
handler. Consider the performance of the application. I'd like use a
lighter routing.

Then, please go ask in comp.unix.programming, as previously suggested.
The question is on-topic there, and you will get more and better
answers.
 
S

Spiro Trikaliotis

Hello,

Alan Balmer said:
[...]

Then, please go ask in comp.unix.programming, as previously suggested.
The question is on-topic there, and you will get more and better
answers.

Why isn't the question OnT here? I reformulate his question (or, at
least, this is mine now ;-)):

Is there a STANDARD way - that is, using only ANSI C - to determine if a
file exists, without creating a new one?

I fail to see why this is OT here.

Regards,
Spiro.
 
G

Gordon Burditt

fopen() has the advantage of existing in standard C, and if
it succeeds, the file exists and you can access it.
[...]

Then, please go ask in comp.unix.programming, as previously suggested.
The question is on-topic there, and you will get more and better
answers.

Why isn't the question OnT here? I reformulate his question (or, at
least, this is mine now ;-)):

Is there a STANDARD way - that is, using only ANSI C - to determine if a
file exists, without creating a new one?

Yes. fopen(filename, "r") .
I fail to see why this is OT here.

The original post brought up functions that are not ANSI C:
lstat() and access().

Gordon L. Burditt
 
K

Keith Thompson

Spiro Trikaliotis said:
Alan Balmer said:
Gordon Burditt wrote:
fopen() has the advantage of existing in standard C, and if
it succeeds, the file exists and you can access it.
[...]

Then, please go ask in comp.unix.programming, as previously suggested.
The question is on-topic there, and you will get more and better
answers.

Why isn't the question OnT here?

Raymond already said that fopen() is not satisfactory for his
purposes, so he needs a system-specific solution.
I reformulate his question (or, at
least, this is mine now ;-)):

Is there a STANDARD way - that is, using only ANSI C - to determine if a
file exists, without creating a new one?

The best standard way to do this is to use fopen() and check whether
it succeeded (and then call fclose() if it didn't). This has some
drawbacks in some cases.

(Depending on the system, it can sometimes be impossible to determine
whether a file exists, for example if you don't have read access to
the directory containing the file.)
 
A

Alan Balmer

Hello,

Alan Balmer said:
Gordon Burditt wrote:
fopen() has the advantage of existing in standard C, and if
it succeeds, the file exists and you can access it.
[...]

Then, please go ask in comp.unix.programming, as previously suggested.
The question is on-topic there, and you will get more and better
answers.

Why isn't the question OnT here? I reformulate his question (or, at
least, this is mine now ;-)):

Is there a STANDARD way - that is, using only ANSI C - to determine if a
file exists, without creating a new one?

I fail to see why this is OT here.
That question, regarding a standard way, has already been answered.

The answer was unsatisfactory to the OP, so he is directed to a group
where more satisfactory answers are topical.
 
S

Spiro Trikaliotis

Hello,

I'll answer only Gordon here, but I thank the other posters, too.

Gordon Burditt said:
Yes. fopen(filename, "r")

Oh yes. I'm sorry, it seems I did not read (or think about) the first
answers here correctly. My mistake!

Thanks,
Spiro.
 
E

Emmanuel Delahaye

Raymond wrote on 27/05/05 :
But I only want to know whether the file exists. If fopen success, The file
is open and a system file handler is assigned. I must close the handler.
Consider the performance of the application. I'd like use a lighter routing.

Design issue. Why do you want to know wether the file 'exist' if it's
not to open if in some way ?

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"Mal nommer les choses c'est ajouter du malheur au
monde." -- Albert Camus.
 

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

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top