473,836 Members | 1,584 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 #1
52 7551
pa****@gmail.co m opined:
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!
You can try reading `errno` (from <errno.h>) do get to the error code,
provided your implementation gives you one, and it's sensible (it's
not required by the Standard to do that). You'd have to set `errno` to
0 before calling `fopen()`, as library functions never do that. You
should consult documentation that came with your C compiler to see
what `errno` codes are available, and whether they can help you.

If your implementation does set useful error codes, you can then use
`strerror()` from <string.h> to get a huan-readable error string, or
`perror()` from <stdio.h>. Look them up in your manual.

Again, if your implementation does not provide sensible error
codes/messages, you're out in the cold. OTH, relying on your
implementation error codes/messages is more likely than not to make
your code non-portable.

--Ever heard of .cshrc?

That's a city in Bosnia. Right?
(Discussion in comp.os.linux.m isc on the intuitiveness of commands.)

<http://clc-wiki.net/wiki/Introduction_to _comp.lang.c>

Apr 16 '06 #2
MH
I think this is what you are looking for :

FILE *fp

main(){

fp=fopen("somet hing.txt","r"); // open for reading
if (!fp) printf ("file doesn't exist");
else prinf("file exist");
fclose(fp);
}

Apr 16 '06 #3

MH wrote:
I think this is what you are looking for :

FILE *fp

main(){

fp=fopen("somet hing.txt","r"); // open for reading
if (!fp) printf ("file doesn't exist");
else prinf("file exist");
fclose(fp);
}


Consider the following:
% touch something.txt
% chmod -r something.txt
% a.out
file doesn't exist

That's not correct.

Apr 16 '06 #4
Ico
MH <mi******@o2.pl > wrote:
I think this is what you are looking for :

FILE *fp

main(){

fp=fopen("somet hing.txt","r"); // open for reading
if (!fp) printf ("file doesn't exist");
else prinf("file exist");
fclose(fp);
}


Sorry to be nitpicking, but this code has some serious flaws: You didn't
include stdio.h, which you need for fopen, fclose and printf. main()
should return an int. Your printf's might not show any output because
you forgot the terminating newlines, and the fclose() causes serious
undefined behaviour when the fopen() fails.

#include <stdio.h>

int main(void)
{
FILE *fp;

fp = fopen("somethin g.txt", "r");

if(fp == NULL) {
printf("file doesn't exist\n");
} else {
printf("file exists\n");
fclose(fp);
}

return 0;
}
--
:wq
^X^Cy^K^X^C^C^C ^C
Apr 16 '06 #5
"Ico" writes:
I think this is what you are looking for :

FILE *fp

main(){

fp=fopen("somet hing.txt","r"); // open for reading
if (!fp) printf ("file doesn't exist");
else prinf("file exist");
fclose(fp);
}


Sorry to be nitpicking, but this code has some serious flaws:


Not to mention that it has nothing whatsoever to do with the question asked.
Apr 16 '06 #6
On 2006-04-16, pa****@gmail.co m <pa****@gmail.c om> wrote:
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


I'm guessing from your pseudo code you already know how to open a file
and set fp etc.

On my system the man page for fopen refers me to "open" which lists
the values that the global "errno" is set to in the event of a failed
file open operation.

According to one man page I checked, fopen was ansi-c compliant. I cant
confirm this one way or another.
good luck
Apr 16 '06 #7
AT first thanks beacause of your informations,bu t I knew these,that you
wrote here.I think I don't explain my question well!
This is my question
What is the correct means of fp=='\0'
(I mean if fp=='\0' is equal to "FILE DOES NOT EXIST" or "you have some
errors <I don't know what but imagine something like you don't have
right permission to open the file>")
thanks

Apr 16 '06 #8
Ico
pa****@gmail.co m wrote:
AT first thanks beacause of your informations,bu t I knew these,that you
wrote here.I think I don't explain my question well!
This is my question
What is the correct means of fp=='\0'
(I mean if fp=='\0' is equal to "FILE DOES NOT EXIST" or "you have some
errors <I don't know what but imagine something like you don't have
right permission to open the file>")
thanks


I believe Vladimir answered this part of your question well : fopen()
returns NULL on error (and NULL is not the same as '\0' !), and you can
inspect errno to find the nature of the error.

--
:wq
^X^Cy^K^X^C^C^C ^C
Apr 16 '06 #9
AT first thanks beacause of your informations,bu t I knew these,that you
wrote here.I think I don't explain my question well!
This is my question
What is the correct means of fp=='\0'
(I mean if fp=='\0' is equal to "FILE DOES NOT EXIST" or "you have some
errors <I don't know what but imagine something like you don't have
right permission to open the file>")
thanks

Apr 16 '06 #10

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

Similar topics

10
17218
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
14415
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
4086
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
2593
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
2144
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
4965
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
19137
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
2058
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
5113
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
9668
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
10840
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
10546
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10588
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
9371
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...
0
5647
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...
0
5823
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4013
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3112
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.