File exist

P

paytam

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
 
V

Vladimir S. Oka

(e-mail address removed) 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.misc on the intuitiveness of commands.)

<http://clc-wiki.net/wiki/Introduction_to_comp.lang.c>
 
M

MH

I think this is what you are looking for :

FILE *fp

main(){

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

Bill Pursell

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

FILE *fp

main(){

fp=fopen("something.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.
 
I

Ico

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

FILE *fp

main(){

fp=fopen("something.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("something.txt", "r");

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

return 0;
}
 
R

Richard G. Riley

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
 
P

paytam

AT first thanks beacause of your informations,but 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

Ico

AT first thanks beacause of your informations,but 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.
 
P

paytam

AT first thanks beacause of your informations,but 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
 
N

Nick Keighley

please leave some context in your post (quote what you are replying
to (as I do))

AT first thanks beacause of your informations,but 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'

you shouldn't test against a null character ('\0') as fp is a pointer.
To be clear you should test against 0 or NULL (my preference)

If fopen() returns a null pointer it means it has failed.
(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>")

there are many reasons fopen() can fail including the file's
non-existance
and file permission problems. Standard C does not distinguish these
reasons but as others have pointed out many implementations (including
Unix) set errno so you could then use strerr() or perror() to find out
why it
failed.
 
J

Joe Wright

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
Consider this..

#include <stdio.h>
#include <errno.h>
#include <stdlib.h>

int main(int argc, char *argv[]) {
FILE *fp = NULL;
char *file = NULL;
errno = 0;
if (argc > 1) {
file = argv[1];
if ((fp = fopen(file, "r+")) == NULL)
perror(file), exit(EXIT_FAILURE);
printf("%s is open\n", file);
fclose(fp);
}
return 0;
}

The Standard makes no guarantees about errno as far as I know but most
implementations will handle it this way. A failing function will set
errno with an integer indicating the type of error (see errno.h). Then
'perror(char *);' will print your message, a colon and a description of
the error.

I open the file with "r+" so that I'll get an access error if the file
is read-only.
 
I

Ico

osmium said:
Not to mention that it has nothing whatsoever to do with the question asked.

I do not agree with that. the O.P. is clearly a beginner at C language,
and code that cause this kind of severe undefined behaviour (especially
fclose()'ing a NULL file descriptor) deserves some attention and should
be corrected. In my humble opinion, causing a segmentation fault,
bluescreen, or whatever symptoms his system may show, is not giving the
O.P. a good feeling about his first C programs.
 
E

ed

On 16 Apr 2006 01:53:03 -0700
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!

Try using stat(5).

NAME
stat, fstat, lstat - get file status

SYNOPSIS
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

int stat(const char *file_name, struct stat *buf);
int fstat(int filedes, struct stat *buf);
int lstat(const char *file_name, struct stat *buf);

DESCRIPTION
These functions return information about the specified file.
You do not need any access rights to the file to get this
information but you need search rights to all directories named
in the path leading to the file.

stat stats the file pointed to by file_name and fills in buf.

See the full man page for further details if you think it might be of
use.
 
O

osmium

Ico said:
I do not agree with that. the O.P. is clearly a beginner at C language,
and code that cause this kind of severe undefined behaviour (especially
fclose()'ing a NULL file descriptor) deserves some attention and should
be corrected. In my humble opinion, causing a segmentation fault,
bluescreen, or whatever symptoms his system may show, is not giving the
O.P. a good feeling about his first C programs.

The OP didn't have any fclose. The *response* had the fclose you don't like.
The OP asked *why* wouldn't the file open? The code posted allegedly told
him how to open a file - something he already knew.

So he got a bad answer to a question he didn't ask. I don't really consider
that helpful to the OP.
 
S

Skarmander

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
In addition to all other answers you got, please read the FAQ: http://c-faq.com.

I recommend reading it all, but your question in particular is question 19.11.

S.
 
I

Ico

osmium said:
The OP didn't have any fclose. The *response* had the fclose you don't like.
The OP asked *why* wouldn't the file open? The code posted allegedly told
him how to open a file - something he already knew.

So he got a bad answer to a question he didn't ask. I don't really consider
that helpful to the OP.

I *do* consider it helpful to comment on broken code - wether posted by
the OP or not - so we seem to disagree on this point. I don't think
there is much we can do about that, I'm sorry.
 
D

Default User

AT first thanks beacause of your informations,but I knew these,that
you wrote here.I think I don't explain my question well!
This is my question

Please read the information below.



Brian
 
K

Keith Thompson

ed said:
On 16 Apr 2006 01:53:03 -0700
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.programmer.
 
R

Rod Pemberton

osmium said:
The OP didn't have any fclose. The *response* had the fclose you don't like.
The OP asked *why* wouldn't the file open? The code posted allegedly told
him how to open a file - something he already knew.

"...allegedly told him..." - since when does code talk? If you want
inanimate or personified talking objects, read the Bible, smoke dope, etc...

"...something he already knew..." - apparently not. The OP's post didn't
contain anything which would indicate that.
So he got a bad answer to a question he didn't ask. I don't really consider
that helpful to the OP.

I proffer that you made some poor assumptions which resulted in faulty
circular logic...


RP
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,183
Messages
2,570,965
Members
47,511
Latest member
svareza

Latest Threads

Top