question about gets() and feof().

S

sunnyboyGuo

hello everyone,
my code is like this:
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
FILE *fp;
char str[128];

if((fp=fopen(argv[1], "r"))==NULL) {
printf("Cannot open file.\n");
exit(1);
}
while(!feof(fp)) {
if(fgets(str, 126, fp))
printf("%s", str);
else
printf("error.\n");
}
fclose(fp);
return 0;
}
////////////////////////////////////////////
if the input file is like this:
abc //first line
ddd //second line. and there is no "enter" in the end.
our code will run correctly.
if the input file is like this:
abc //first line
ddd //second line. in the end, press "enter"
//last line, this line is press "enter" in the second line.
//and there is none other key in the last line.
in this case, my code will output "error".

what's wrong?

sincerely

guo
 
C

Christopher Benson-Manica

#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
FILE *fp;
char str[128];
if((fp=fopen(argv[1], "r"))==NULL) {

What if argc is 1?
printf("Cannot open file.\n");
exit(1);

exit( EXIT_FAILURE );
}
while(!feof(fp)) {
if(fgets(str, 126, fp))

if( fgets(str,sizeof str,fp) )
printf("%s", str);
else
printf("error.\n");
}
fclose(fp);
return 0;
}
what's wrong?

You have asked a FAQ:

http://www.eskimo.com/~scs/C-faq/q12.2.html
 
K

Kenneth Brody

while(!feof(fp)) {
if(fgets(str, 126, fp))
printf("%s", str);
else
printf("error.\n");
} [...]
if the input file is like this:
abc //first line
ddd //second line. in the end, press "enter"
//last line, this line is press "enter" in the second line.
//and there is none other key in the last line.
in this case, my code will output "error".

what's wrong?

A NULL return from fgets does not necessarily mean "error".

fgets() returns NULL if EOF is hit before reading anything. Since there
is nothing between the newline after "ddd" on the second line and EOF,
there are no more characters left to read. Hence NULL is returned even
though no error has occurred.

See also <http://www.eskimo.com/~scs/C-faq/q12.2.html> for further info
on another problem which you haven't hit yet.

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <mailto:[email protected]>
 
S

Simon Biber

hello everyone,
my code is like this:
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
FILE *fp;
char str[128];

if((fp=fopen(argv[1], "r"))==NULL) {
printf("Cannot open file.\n");
exit(1);
}
while(!feof(fp)) {
if(fgets(str, 126, fp))
printf("%s", str);
else
printf("error.\n");
}

The logic of this while loop is wrong. Replace it by:

while(fgets(str, sizeof str, fp))
{
printf("%s", str);
}
if(ferror(fp))
{
printf("error.\n");
}

fgets will also return NULL if end-of-file occurs before reading any
data in. This is not an error condition. You can tell if an error
occurred by calling ferror.
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top