473,767 Members | 2,302 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

File exist

Hi all
Can anyone tell me how can I check that a file exist or no.I mean when
you use this commands
FILE *fp;
if(!fp)
//Could not open the file
doen't show why it can not open it,may be the file doesn't exist.Now
tell me what should I do!
Thanks

Apr 16 '06
52 7536
Keith Thompson wrote:
ed <ed@noreply.com > writes:
On 16 Apr 2006 01:53:03 -0700
pa****@gmail.co m wrote:
Can anyone tell me how can I check that a file exist or no.

[...]
Try using stat(5).


There is no stat() function in standard C. Using it will limit the
portability of your code. See comp.unix.progr ammer.


I learnt from others that there is a function named access can be used
to detect if a dedicated file exists. The function is as follows:

int access (char* filename, int mode)

For instance, if I wanted to know whether /opt/test.log exists, I would
call this function as
int flag = access("/opt/test.log", 0);

However, this function is included in <io.h>, perhaps it's not a
standard function either.

Apr 18 '06 #21
Claude Yih wrote:
Keith Thompson wrote:
ed <ed@noreply.com > writes:
On 16 Apr 2006 01:53:03 -0700
pa****@gmail.co m wrote:
Can anyone tell me how can I check that a file exist or no.

[...]
Try using stat(5).

There is no stat() function in standard C. Using it will limit the
portability of your code. See comp.unix.progr ammer.


I learnt from others that there is a function named access can be used
to detect if a dedicated file exists. The function is as follows:

int access (char* filename, int mode)

For instance, if I wanted to know whether /opt/test.log exists, I would
call this function as
int flag = access("/opt/test.log", 0);

However, this function is included in <io.h>, perhaps it's not a
standard function either.


It is not part of standard C. As I'm sure others have said in this
thread standard C does not provide any portable method to see if a file
exists. So you can be sure that any method you find goes beyond what
standard C provides.
--
Flash Gordon, living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidelines and intro:
http://clc-wiki.net/wiki/Intro_to_clc

Inviato da X-Privat.Org - Registrazione gratuita http://www.x-privat.org/join.php
Apr 18 '06 #22
In article <76************ @news.flash-gordon.me.uk>, Flash Gordon
<sp**@flash-gordon.me.uk> wrote:

Claude Yih wrote:
Keith Thompson wrote:
ed <ed@noreply.com > writes:
On 16 Apr 2006 01:53:03 -0700
pa****@gmail.co m wrote:
> Can anyone tell me how can I check that a file exist or no.
[...]
Try using stat(5).
There is no stat() function in standard C. Using it will limit the
portability of your code. See comp.unix.progr ammer.


I learnt from others that there is a function named access can be used
to detect if a dedicated file exists. The function is as follows:

int access (char* filename, int mode)

For instance, if I wanted to know whether /opt/test.log exists, I would
call this function as
int flag = access("/opt/test.log", 0);

However, this function is included in <io.h>, perhaps it's not a
standard function either.


It is not part of standard C. As I'm sure others have said in this
thread standard C does not provide any portable method to see if a file
exists. So you can be sure that any method you find goes beyond what
standard C provides.


What is wrong with attempting to open the file and if there is no error
closing it again? Surely this is standard 'C'?

John
--
_ _______________ _______________ ___________
/ \._._ |_ _ _ /' Orpheus Internet Services
\_/| |_)| |(/_|_|_> / 'Internet for Everyone'
_______ | ___________./ http://www.orpheusinternet.co.uk
Apr 18 '06 #23
Mr John FO Evans wrote:
[...]
>> On 16 Apr 2006 01:53:03 -0700
>> pa****@gmail.co m wrote:
>>> Can anyone tell me how can I check that a file exist or no.
[...] What is wrong with attempting to open the file and if there is no error
closing it again? Surely this is standard 'C'?


Because if it fails, it won't (necessarily) tell you why. As has been
pointed out elsethread, insufficient access privileges will fail the
same way as a non-existent file. While many implementations will set
errno to the reason for the failure, such behavior is not guaranteed by
the Standard.

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer .h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <mailto:Th***** ********@gmail. com>

Apr 18 '06 #24
Mr John FO Evans <mi***@orpheusm ail.co.uk> writes:
In article <76************ @news.flash-gordon.me.uk>, Flash Gordon
<sp**@flash-gordon.me.uk> wrote:

[...]
It is not part of standard C. As I'm sure others have said in this
thread standard C does not provide any portable method to see if a file
exists. So you can be sure that any method you find goes beyond what
standard C provides.


What is wrong with attempting to open the file and if there is no error
closing it again? Surely this is standard 'C'?


Yes, but it only tells you whether you were able to open the file; it
doesn't necessarily tell you why. (And fopen() doesn't even
necessarily set errno to a meaningful value.)

On some systems, it might be possible to open a file in binary mode
but not in text mode, or vice versa.

In some cases, it's not even possible to determine whether a file
exists; you might not have permission to ask (e.g., if the file is in
a directory you don't have permission to read). Also, a file might
exist when you test it, but cease to exist before you use it.

Usually the best approach is to try to open the file, and handle the
error if the attempt fails. Providing information about *why* it
failed can be useful, but it isn't absolutely necessary, and it can't
be done portably.

--
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.
Apr 18 '06 #25
Mr John FO Evans wrote:
In article <76************ @news.flash-gordon.me.uk>, Flash Gordon
<sp**@flash-gordon.me.uk> wrote:
Claude Yih wrote:
Keith Thompson wrote:

ed <ed@noreply.com > writes:
> On 16 Apr 2006 01:53:03 -0700
> pa****@gmail.co m wrote:
>> Can anyone tell me how can I check that a file exist or no.
[...]
> Try using stat(5).
There is no stat() function in standard C. Using it will limit the
portability of your code. See comp.unix.progr ammer.
I learnt from others that there is a function named access can be used
to detect if a dedicated file exists. The function is as follows:

int access (char* filename, int mode)

For instance, if I wanted to know whether /opt/test.log exists, I would
call this function as
int flag = access("/opt/test.log", 0);

However, this function is included in <io.h>, perhaps it's not a
standard function either.

It is not part of standard C. As I'm sure others have said in this
thread standard C does not provide any portable method to see if a file
exists. So you can be sure that any method you find goes beyond what
standard C provides.


What is wrong with attempting to open the file and if there is no error
closing it again? Surely this is standard 'C'?


It's standard C but it does not prove the file does not exist. It might
exist but your have permission set preventing you from opening it
(perhaps a different user owns it) or another process might have it
opened exclusively, or...
--
Flash Gordon, living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidelines and intro:
http://clc-wiki.net/wiki/Intro_to_clc
Apr 18 '06 #26
Keith Thompson wrote:
[...]
Usually the best approach is to try to open the file, and handle the
error if the attempt fails. Providing information about *why* it
failed can be useful, but it isn't absolutely necessary, and it can't
be done portably.


Isn't this portable?

... proper #include's, etc. implied ...

errno=0;
f = fopen(filename, mode);
if ( f == NULL )
{
if ( errno != 0 )
perror(filename );
else
fprintf(stderr, "fopen() of %s failed.\n",file name);
exit(EXIT_FAILU RE);
}
--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer .h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <mailto:Th***** ********@gmail. com>

Apr 18 '06 #27
Kenneth Brody <ke******@spamc op.net> writes:
Keith Thompson wrote:
[...]
Usually the best approach is to try to open the file, and handle the
error if the attempt fails. Providing information about *why* it
failed can be useful, but it isn't absolutely necessary, and it can't
be done portably.


Isn't this portable?

... proper #include's, etc. implied ...

errno=0;
f = fopen(filename, mode);
if ( f == NULL )
{
if ( errno != 0 )
perror(filename );
else
fprintf(stderr, "fopen() of %s failed.\n",file name);
exit(EXIT_FAILU RE);
}


No, since the standard doesn't say that fopen() sets errno on failure.
perror() could print something silly like "foo.txt: No error", or
"foo.txt: File exists".

--
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.
Apr 18 '06 #28
Kenneth Brody wrote:
Keith Thompson wrote:
[...]
Usually the best approach is to try to open the file, and handle the
error if the attempt fails. Providing information about *why* it
failed can be useful, but it isn't absolutely necessary, and it can't
be done portably.


Isn't this portable?

... proper #include's, etc. implied ...

errno=0;
f = fopen(filename, mode);
if ( f == NULL )
{
if ( errno != 0 )
perror(filename );
else
fprintf(stderr, "fopen() of %s failed.\n",file name);
exit(EXIT_FAILU RE);
}


It's portable, but it might always print out "fopen() of %s failed.\n"
and never give a reason on some implementations .
--
Flash Gordon, living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidelines and intro:
http://clc-wiki.net/wiki/Intro_to_clc
Apr 18 '06 #29

After reading all the comments on this thread I am astounded that the 'C'
standard does not require that fopen returns a meaningful error when it
fails. Surly this is an ommision - or perhaps it is a acceptance that
computer systems suppliers do not like standards and hence do not provide
meaningful error returns from their lower level routines?

John
--
_ _______________ _______________ ___________
/ \._._ |_ _ _ /' Orpheus Internet Services
\_/| |_)| |(/_|_|_> / 'Internet for Everyone'
_______ | ___________./ http://www.orpheusinternet.co.uk
Apr 19 '06 #30

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

Similar topics

10
17208
by: lamar_air | last post by:
I have a python script and i want to delete a file on my computer c:\....\...\.. .txt what's the script to do this? I know how to write to files but i also need to know how to delete a file.
2
14409
by: Chris Fink | last post by:
I am using the System.IO.File class to determine if a file exists on a network share. The File.Exists method keeps returning false, even though the file does exist. The MSDN documentation states, "If the caller does not have sufficient permissions to read the specified file, no exception is thrown and the method returns false regardless of the existence of path."
4
4081
by: Mike | last post by:
Hi, I am looking for function in .Net library that let me know if exist any file if I specified template. Eg: I specify "*.txt" and if any file (1.txt, 2.txt, .. ) exists then I can get True or at least file name . And if does not exist Fasle or empty string. Of course I can use VB6 function Dir, but maybe .Net contains
1
2592
by: Tim Failes | last post by:
This seems a trival question, but I cannot get it to work properly... Essentially my question is, how can I create a text file, and guarantee it is given the current date/time as the Creation Time? My code is creating a file correctly, but the CreationTime is (sometimes) set to the CreationTime of an old file that has previously been removed. The line of code creating the file is _LogFileWriter = newFile.CreateText()
3
2142
by: tshad | last post by:
I have a function that downloads a file to the users computer and it works fine. The problem is that I then want the program to rename the file (file.move) to the same name plus todays date. The problem is that when you run the program it gets to the: Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name)
26
4957
by: Army1987 | last post by:
Is this a good way to check wheter a file already exists? #include <stdio.h> #include <stdlib.h> int ask(const char *prompt); typedef char filename; int main(int argc, char *argv) { FILE *in, *out;
7
19116
by: sprash | last post by:
Newbie question: I'm trying to determine if a file physically exists regardless of the permissions on it Using File.Exists() returns false if it physically exists but the process does not have the necessary permissions. One hack could be to check for length and that would throw a FileNotFoundException ...but there is got to be a better way!
3
2052
by: brook | last post by:
hey all - i´m new to php and having trouble writing a simple code which should create a file. here is the most simplified version: <?php $content = "my content"; $path = "test.txt"; if(!chmod($path, 0744)) { echo "error, $path"; exit; } else {
65
5094
by: Hongyu | last post by:
Dear all: I am trying to write to a file with full directory name and file name specified (./outdir/mytestout.txt where . is the current directory) in C programming language and under Unix, but got errors of Failed to open file ./outdir/mytestout.txt. Below is the code: #include <stdio.h>
0
9571
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...
0
9404
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
9838
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
8835
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
7381
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
6651
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
5423
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3929
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
2
3532
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.