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

How to check file exists.

P: n/a
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.
Nov 14 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a
On Fri, 27 May 2005 11:35:43 +0800, Raymond <rs*******@yahoo.com>
wrote in comp.lang.c:
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 news:comp.unix.programmer.

--
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 14 '05 #2

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

P: n/a
Gordon Burditt wrote:
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.


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.

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

Nov 14 '05 #4

P: n/a
On Fri, 27 May 2005 13:45:17 +0800, Raymond <rs*******@yahoo.com>
wrote:
Gordon Burditt wrote:
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.


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.

--
Al Balmer
Balmer Consulting
re************************@att.net
Nov 14 '05 #5

P: n/a
Hello,

Alan Balmer <al******@att.net> wrote:
On Fri, 27 May 2005 13:45:17 +0800, Raymond <rs*******@yahoo.com>
wrote:
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.

Regards,
Spiro.

--
Spiro R. Trikaliotis http://cbm4win.sf.net/
http://www.trikaliotis.net/ http://www.viceteam.org/
Nov 14 '05 #6

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

P: n/a
Spiro Trikaliotis <ne*********@trikaliotis.net> writes:
Alan Balmer <al******@att.net> wrote:
On Fri, 27 May 2005 13:45:17 +0800, Raymond <rs*******@yahoo.com>
wrote:
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.)

--
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 14 '05 #8

P: n/a
On Fri, 27 May 2005 18:55:10 +0200, Spiro Trikaliotis
<ne*********@trikaliotis.net> wrote:
Hello,

Alan Balmer <al******@att.net> wrote:
On Fri, 27 May 2005 13:45:17 +0800, Raymond <rs*******@yahoo.com>
wrote:
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.

--
Al Balmer
Balmer Consulting
re************************@att.net
Nov 14 '05 #9

P: n/a
Hello,

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

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


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

Thanks,
Spiro.

--
Spiro R. Trikaliotis http://cbm4win.sf.net/
http://www.trikaliotis.net/ http://www.viceteam.org/
Nov 14 '05 #10

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

Nov 14 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.