473,378 Members | 1,510 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

How to check file exist using c language

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

Nov 15 '05 #1
7 33448
>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!


fopen()

There is no absolutely correct way to check whether a file exists
or not, since the security rules of the OS may not permit you
to know whether the file exists or not. But fopen() in "r"
mode gives you a definite YES or a probable NO.

Gordon L. Burditt
Nov 15 '05 #2
go****@hammy.burditt.org (Gordon Burditt) writes:
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!


fopen()

There is no absolutely correct way to check whether a file exists
or not, since the security rules of the OS may not permit you
to know whether the file exists or not. But fopen() in "r"
mode gives you a definite YES or a probable NO.


On the other hand, the information may not be particularly useful.
You can write a function that attempts to fopen() a file, records
whether it succeeded, fclose()s the file, and returns the result of
the fopen(). The file may no longer exist by the time you try to use
it. For that matter, an fopen() call has to specify whether the file
is to be opened in text or binary mode. If the OS makes a strong
distinction between text and binary files, it may be that one will
succeed and the other will fail. And, of course, knowing that a file
exists doesn't guarantee that you can do anything with it.

Presumably you want to know whether a file exists so you can find out
whether you can do something with it. Often a better approach is to
go ahead and try to do what you wanted to do in the first place, and
handle any errors that result.

--
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 15 '05 #3

"Gordon Burditt" <go****@hammy.burditt.org> wrote
There is no absolutely correct way to check whether a file exists
or not, since the security rules of the OS may not permit you
to know whether the file exists or not. But fopen() in "r"
mode gives you a definite YES or a probable NO.

However if you close the file, and then try to open it again in the next
statement, it may have disappeared.
Nov 15 '05 #4
Is there any example availabe for my reference? Thanks a lot

Malcolm wrote:
"Gordon Burditt" <go****@hammy.burditt.org> wrote
There is no absolutely correct way to check whether a file exists
or not, since the security rules of the OS may not permit you
to know whether the file exists or not. But fopen() in "r"
mode gives you a definite YES or a probable NO.

However if you close the file, and then try to open it again in the next
statement, it may have disappeared.


Nov 15 '05 #5
On 6 Jul 2005 13:55:38 -0700, an*****@hotmail.com wrote in
comp.lang.c:
Hi all:
The c++ language can check whether the file exist or not.
No, it cannot. And neither can the C++ standard library, which is
much more pertinent. The person who told you this is mistaken. Even
if you yourself are that person.
I am
wondering how c language does this job? Thanks a lot!


C does not do this job, neither the language nor the standard library.

--
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 15 '05 #6
an*****@hotmail.com writes:
Malcolm wrote:
"Gordon Burditt" <go****@hammy.burditt.org> wrote
> There is no absolutely correct way to check whether a file exists
> or not, since the security rules of the OS may not permit you
> to know whether the file exists or not. But fopen() in "r"
> mode gives you a definite YES or a probable NO.
>

However if you close the file, and then try to open it again in the next
statement, it may have disappeared.


Is there any example availabe for my reference? Thanks a lot


Please don't top-post. I've corrected it here.

What kind of example do you have in mind?

If a program opens a file, then closes it, then opens the same file
again, there must be some finite time between closing it and
re-opening it. Another program running on the same system could
delete the file during that gap. (This assumes a system on which
multiple programs can execute simultaneously.)

--
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 15 '05 #7

"Keith Thompson" <ks***@mib.org> wrote
If a program opens a file, then closes it, then opens the same file
again, there must be some finite time between closing it and
re-opening it. Another program running on the same system could
delete the file during that gap. (This assumes a system on which
multiple programs can execute simultaneously.)

Or a system with a floppy. If the user presses the eject button, you might
just get unlucky. Probably not worth bothering about for a video game, but
if the program is dealing with medical data it wouldn't be acceptable.
Nov 15 '05 #8

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

Similar topics

2
by: Lin Ma | last post by:
Greetings, Is it possbile to check a file exist without using Server.CreateObject("Scripting.FileSystemObject") in asp page?? The reason is our hosting company turn that function off for...
2
by: Mike | last post by:
I´ve got a number of SPAN elements named "mySpan1", "mySpan2", "mySpan3" etc, and want to set their "style.display" to "inline". This works (only needs to work on IE5.5+): for (var x = 1; x <...
2
by: adam | last post by:
hello What query shoul I send to SQL serwer ( in transact SQL language ) to check does some database exist on serwer ? It similar to problem "does some table exist in database" - resolve to it...
4
by: cfyam | last post by:
How can I check some file is exist or not in my evc dll?
3
by: Dave | last post by:
How can i check to see if a link (file) exist while a web page is loading. If the page doesn't exist - i want the hyperlink to go an error page telling the user the link doesn't exist otherwise...
10
by: Lasse Kärkkäinen / Tronic | last post by:
Hi, While people are getting all excited of new features that will extend the language, I'm more worried about basic stuff that is important in a very large number of applications: file I/O. ...
2
by: Miro | last post by:
I will ask the question first then fumble thru trying to explain myself so i dont waste too much of your time. Question / Statement - Every mdb table needs a PrimaryKey ( or maybe an index - i...
3
by: gr8graphix | last post by:
My skills are basic at best, but I can normally edit existing codes to suite my needs and someday hope to be good at it (any good coaches out there?)... Any way, I am working on this piece of code...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.