Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > question about gets() and feof().

Reply
Thread Tools

question about gets() and feof().

 
 
sunnyboyGuo@gmail.com
Guest
Posts: n/a
 
      09-16-2005
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

 
Reply With Quote
 
 
 
 
Christopher Benson-Manica
Guest
Posts: n/a
 
      09-16-2005
wrote:

> #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

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
 
Reply With Quote
 
 
 
 
Kenneth Brody
Guest
Posts: n/a
 
      09-16-2005
wrote:
[...]
> 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: <private.php?do=newpm&u=>


 
Reply With Quote
 
Simon Biber
Guest
Posts: n/a
 
      09-16-2005
wrote:
> 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.

> fclose(fp);
> return 0;
> }


--
Simon.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
question about .h file and .cpp file,also compiler question key9 C++ 7 09-13-2006 06:45 PM
Thanks (and apologies) for your comments on "The Most Obvious Question You Are Ever Likely To See" (e.g. No-Brainer security question) Curious George Computer Security 1 03-14-2005 10:01 PM
Re: General USB Printer setup question, and situation-specific question AG A+ Certification 0 01-13-2005 11:13 PM
Three question which is not yet answered clearly and correct so far !! challenging question in xsl and also in xsl fo Philip Meyer XML 0 11-30-2003 04:42 PM
newbie delurking, a lighting question, and a pricing question verminiusrex Digital Photography 2 08-08-2003 10:10 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57