what's wrong with this

R

rsk

Hi Friends,

Can you plzz tell me whats wrong with this code.

#include <stdio.h>
#include <math.h>

main () {

FILE *fp;

fp = fopen("file.txt", "r");
if(fp==NULL){
printf("\n Cannot open file.txt file.\n");
exit(1);
}
else {
printf("\n file.txt File is Opened.\n");

}
}

The file.txt is not having any value but surprisingly iam getting the
following message when i run the above code

file.txt File is Opened.

Can you please tell me why iam getting this?

Thanks in advance.
ss..
 
J

Joachim Schmitz

rsk said:
Hi Friends,

Can you plzz tell me whats wrong with this code.

#include <stdio.h>
#include <math.h>

main () {
int main(void)
FILE *fp;

fp = fopen("file.txt", "r");
if(fp==NULL){
printf("\n Cannot open file.txt file.\n");
exit(1);
}
else {
printf("\n file.txt File is Opened.\n");

}
return 0;
}

The file.txt is not having any value but surprisingly iam getting the
? You mean that file is empty? Or it doesn't exist?
following message when i run the above code

file.txt File is Opened.

Can you please tell me why iam getting this?
Because it got opened successfully. Which means that the file existed before
(or your implementation has a bug)


Bye, Jojo
 
J

Joachim Schmitz

Joachim Schmitz said:
int main(void)

return 0;

? You mean that file is empty? Or it doesn't exist?

Because it got opened successfully. Which means that the file existed
before (or your implementation has a bug)
7.19.5.3-4
Opening a file with read mode ('r' as the first character in the mode
argument) fails if the file does not exist or cannot be read.
 
S

santosh

rsk said:
Hi Friends,

Can you plzz tell me whats wrong with this code.

#include <stdio.h>
#include <math.h>

You don't use anything in math.h
main () {

Better:
int main(void) { /* ... */ }
FILE *fp;

fp = fopen("file.txt", "r");
if(fp==NULL){
printf("\n Cannot open file.txt file.\n");
exit(1);

One is not a portable status value. Use one of 0, EXIT_SUCCESS or
EXIT_FAILURE. The latter two are defined in stdlib.h. The first two are
more or less interchangeable.
}
else {
printf("\n file.txt File is Opened.\n");

}

And return a value from main.
}

The file.txt is not having any value but surprisingly iam getting the
following message when i run the above code

file.txt File is Opened.

Can you please tell me why iam getting this?

Are you sure the file did not exist. Or does it exist, but is empty. If the
former case, then your implementation is broken, but it's much probably the
latter case.
 
R

rsk

hi Friends,

Actually the file.txt exists but is not having any value.

Actually my requirement is to check whether the files file.txt exists or
not and then to see whether it has any data with it.

For that how should i modify my code?

Thanks in advance.
ss...
 
J

Joachim Schmitz

rsk said:
hi Friends,

Actually the file.txt exists but is not having any value.

Actually my requirement is to check whether the files file.txt exists or
not and then to see whether it has any data with it.
if fopen("file.txt", "r") succeeds, the file exists and you'r allowed to
read it
if you can fread() more than 0 bytes, it contains some data

in POSIX there's stat(), which would check existence and at the same time
tell you everything about the file you ever wanted to know, including it's
lenght

Bye, Jojo
 
R

Richard Tobin

Actually my requirement is to check whether the files file.txt exists or
not and then to see whether it has any data with it.
[/QUOTE]
If fopen("file.txt", "r") succeeds, then the file exists. Otherwise, it
does not.

.... or maybe it exists but you don't have the necessary permission to
read it, or any of a zillion other reasons. But quite likely it's the
right thing to do anyway, and it's the requirement that's imprecise.

-- Richard
 
K

Keith Thompson

rsk said:
Can you plzz tell me whats wrong with this code.

"please", not "plzz", please.
#include <stdio.h>
#include <math.h>

main () {

FILE *fp;

fp = fopen("file.txt", "r");
if(fp==NULL){
printf("\n Cannot open file.txt file.\n");
exit(1);
}
else {
printf("\n file.txt File is Opened.\n");

}
}

The file.txt is not having any value but surprisingly iam getting the
following message when i run the above code

file.txt File is Opened.

Can you please tell me why iam getting this?

Others have pointed out the various errors in your code; you should
fix them, but they're probably not causing the problem you're asking
about.

When I read this, I had no idea what you meant by "The file.txt is not
having any value". From the followups, I think you really meant that
file.txt is empty. An empty file is perfectly valid, and is a very
different thing from a file that doesn't exist.

If you want to know whether the file is empty, try to read some data
from it; if you immediately hit the end of the file, it's empty.
 
J

John Gordon

In said:
Actually the file.txt exists but is not having any value.
Actually my requirement is to check whether the files file.txt exists or
not and then to see whether it has any data with it.
For that how should i modify my code?

If fopen("file.txt", "r") succeeds, then the file exists. Otherwise, it
does not.

If a single fgetc() succeeds, then the file has some data in it. If not,
then it doesn't.
 
A

Army1987

If fopen("file.txt", "r") succeeds, then the file exists. Otherwise, it
does not.

If a single fgetc() succeeds, then the file has some data in it. If not,
then it doesn't.

Or better: otherwise, if feof() is true, the file is empty;
if ferror() is true, there was an I/O error.
 
C

CBFalconer

Army1987 said:
John Gordon wrote:
.... snip ...


Or better: otherwise, if feof() is true, the file is empty;
if ferror() is true, there was an I/O error.

No, because feof only shows the result after getc() (or equivalent)
has returned EOF.
 
S

santosh

CBFalconer said:
No, because feof only shows the result after getc() (or equivalent)
has returned EOF.

I think that's what Army1987 meant, i.e., if feof returns true after fgetc
fails, then the file can be considered empty, otherwise if ferror is true,
then an I/O error occurred.
 
Joined
Sep 26, 2007
Messages
5
Reaction score
0
open file in "r" mode, may be this error(s):
1- file dose not exist
2- no permission to read
3- disk are write protected
4- file is empty
... and
your forget "int" before main() definition and "return 0;" in last line.

hamed vahedi
www.hamedvahedi.com
 

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
473,755
Messages
2,569,536
Members
45,008
Latest member
HaroldDark

Latest Threads

Top