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