473,734 Members | 2,764 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to check file exists.

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
10 4514
On Fri, 27 May 2005 11:35:43 +0800, Raymond <rs*******@yaho o.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.l earn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #2
>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
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
On Fri, 27 May 2005 13:45:17 +0800, Raymond <rs*******@yaho o.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.progr amming, 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
Hello,

Alan Balmer <al******@att.n et> wrote:
On Fri, 27 May 2005 13:45:17 +0800, Raymond <rs*******@yaho o.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.progr amming, 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
>>>> 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.progr amming, 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
Spiro Trikaliotis <ne*********@tr ikaliotis.net> writes:
Alan Balmer <al******@att.n et> wrote:
On Fri, 27 May 2005 13:45:17 +0800, Raymond <rs*******@yaho o.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.progr amming, 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_Keit h) 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
On Fri, 27 May 2005 18:55:10 +0200, Spiro Trikaliotis
<ne*********@tr ikaliotis.net> wrote:
Hello,

Alan Balmer <al******@att.n et> wrote:
On Fri, 27 May 2005 13:45:17 +0800, Raymond <rs*******@yaho o.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.progr amming, 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
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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
34744
by: newcomer | last post by:
Is there a way to check if a file exists in Javascript? This is what I'm trying to do: if(thisfile.htm exists) do this else do that
7
33469
by: andylcx | last post by:
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
5
2753
by: jez123456 | last post by:
Hi, I’ve written a c# program to compact certain msaccess databases. The way this works is to compact say C:\test1.mdb to C:\temp.mdb, then delete C:\test1.mdb and rename C:\temp.mdb as C:\test1.mdb. My problem occurs if there is already a C:\temp.mdb file. I don’t want to just delete this, as it could have been created separately by a user. In my code, I set a constant as follows: private const string tempMdb = @"C:\temp.mdb";
2
2452
by: Adrian | last post by:
hi I'm writing, or trying to! an ASP page that checks if a file exists and if so transfers it. the use sends a query string with the name of the file, I have this bit working, but need to check if the path and or file exists in vb6 I would write if dir(strFileName & "\test.zip")) <>"" then msgbox("OK 1 ") else msgbox("OK 2 ")
10
14388
by: Ricardo Luceac | last post by:
Hi all. I'm having a problem with this, I have look if a file exists, if don't wait till it is created and if it exists I need to open it. I do the following: for (; ; ) {
5
11106
by: sword | last post by:
How can I check whether a file exists?
14
46241
by: John Salerno | last post by:
What is the best way to check if a file already exists in the current directory? I saw os.path.isfile(), but I'm not sure if that does more than what I need. I just want to check if a file of a certain name exists before the user creates a new file of that name. Thanks.
3
4168
by: trint | last post by:
How can I do this with my c# code with my website(because the file is there, but the code doesn't return it)?: if(File.Exists(String.Format("~/images/categories/{0}", sFileName)) return String.Format("~/images/categories/{0}", sFileName); else if(File.Exists(String.Format("~/images/products/{0}", sFileName)) return String.Format("~/images/products/{0}", sFileName); else return "";
2
9843
Manikgisl
by: Manikgisl | last post by:
HI. How to check File exists in Web Share C# try { WebRequest request = HttpWebRequest.Create("http://www.microsoft.com/NonExistantFile.aspx"); request.Method = "HEAD"; // Just get the document headers, not the data. request.Credentials = System.Net.CredentialCache.DefaultCredentials; // This may throw a WebException:
0
8776
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9449
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9182
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8186
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6735
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6031
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4809
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2724
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2180
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.