473,587 Members | 2,463 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 33460
>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.bu rditt.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_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 15 '05 #3

"Gordon Burditt" <go****@hammy.b urditt.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.b urditt.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.l earn.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.b urditt.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_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 15 '05 #7

"Keith Thompson" <ks***@mib.or g> 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
7103
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 security reason. Here is my original code: <%
2
6014
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 < 20; x++) { document.all('mySpan'+x).style.display = "inline"; } But I don´t know how many SPAN elements there are, so I need to set x to a
2
17887
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 is query: use db_silnik IF EXISTS (SELECT * FROM prad) PRINT 'table exist'
4
15300
by: cfyam | last post by:
How can I check some file is exist or not in my evc dll?
3
4866
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 they will just go the link which is a pdf file. I'm using asp.net 2003. thanks Dave
10
2031
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. 1) 64 bit (or large enough) addressing on 32 bit systems. Files obviously are getting bigger and 32 bit addressing simply isn't enough anymore and the situation is only getting worse. This drives people into using platform-dependant I/O and thus...
2
3098
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 havnt tested the index yet ) so you can use an .UPDATE( dataTable ) on the data adapter. Otherwise you will get an exception error. Is this statement true? ---- Now me fumbling thru
3
1643
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 that pulls in all the echos from a language include. Works fine but it generates dozens of "undefined variable" warnings each time it goes to the language file for another piece of text. For instance, the language file contains: $l_admin18 =...
0
7924
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
7978
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8221
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...
1
5722
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
5395
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
3845
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2364
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1455
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1192
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.