By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,626 Members | 2,169 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,626 IT Pros & Developers. It's quick & easy.

what's wrong with this

P: n/a
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..
--
Message posted using http://www.talkaboutprogramming.com/group/comp.lang.c/
More information at http://www.talkaboutprogramming.com/faq.html

Sep 28 '07 #1
Share this Question
Share on Google+
11 Replies


P: n/a

"rsk" <kr******@yahoo.co.inschrieb im Newsbeitrag
news:ee******************************@localhost.ta lkaboutprogramming.com...
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
Sep 28 '07 #2

P: n/a

"Joachim Schmitz" <no*********@schmitz-digital.deschrieb im Newsbeitrag
news:fd**********@online.de...
>
"rsk" <kr******@yahoo.co.inschrieb im Newsbeitrag
news:ee******************************@localhost.ta lkaboutprogramming.com...
>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)
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.
Sep 28 '07 #3

P: n/a
rsk wrote:
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.
Sep 28 '07 #4

P: n/a
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...

--
Message posted using http://www.talkaboutprogramming.com/group/comp.lang.c/
More information at http://www.talkaboutprogramming.com/faq.html

Sep 28 '07 #5

P: n/a
"rsk" <kr******@yahoo.co.inschrieb im Newsbeitrag
news:dd******************************@localhost.ta lkaboutprogramming.com...
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
Sep 28 '07 #6

P: n/a
"rsk" <kr******@yahoo.co.inwrites:
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.

--
Keith Thompson (The_Other_Keith) 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."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Sep 28 '07 #7

P: n/a
In <dd******************************@localhost.talkab outprogramming.com"rsk" <kr******@yahoo.co.inwrites:
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.

--
John Gordon A is for Amy, who fell down the stairs
go****@panix.com B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"

Sep 28 '07 #8

P: n/a
In article <fd**********@reader1.panix.com>,
John Gordon <go****@panix.comwrote:
>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, 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
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Sep 28 '07 #9

P: n/a
On Fri, 28 Sep 2007 20:31:06 +0000, John Gordon wrote:
In <dd******************************@localhost.talkab outprogramming.com"rsk" <kr******@yahoo.co.inwrites:
>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.
Or better: otherwise, if feof() is true, the file is empty;
if ferror() is true, there was an I/O error.

--
Army1987 (Replace "NOSPAM" with "email")
A hamburger is better than nothing.
Nothing is better than eternal happiness.
Therefore, a hamburger is better than eternal happiness.

Sep 29 '07 #10

P: n/a
Army1987 wrote:
John Gordon wrote:
.... snip ...
>
>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.
No, because feof only shows the result after getc() (or equivalent)
has returned EOF.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>

--
Posted via a free Usenet account from http://www.teranews.com

Sep 29 '07 #11

P: n/a
CBFalconer wrote:
Army1987 wrote:
>John Gordon wrote:
... snip ...
>>
>>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.

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.

Sep 30 '07 #12

This discussion thread is closed

Replies have been disabled for this discussion.