Liste des Groupes | Revenir à cl c |
Janis Papanagnou wrote:Ah, I see Ben covered that earlier today elsewhere in the thread.I have a _coherent_ file, with a few NL terminated lines of text....
>
Usually I use fgets() in contexts where I process every line, like
>
while (fgets (buf, BUFSIZ, fd) != NULL) {
operate_on (buf);
}
// here the status of buf[] is usually not important any more
>
My actual context was different, like
>
while (fgets (buf, BUFSIZ, fd) != NULL) {
// buf[] contents are ignored here
}
operate_on (buf[]); // which I assumed contains last line
Usually I read and process the data that I got in buf from fgets()What does fgets do if the file is completely empty? I may be wrong (more familiar with Python than C these days), but it doesn't look like that should be any different from any other end-of-file condition, so presumably the first call to fgets would return NULL, without ever modifying the buffer. Unless the buffer is initialised (e.g. to an empty string) before the while loop, that would result in an uninitialised buffer being passed to operate_on.
while there *is* data (fgets() != NULL), and I thus don't care any
more about buffer contents validity after the loop (fgets() == NULL).
>
But now I wanted to ignore all data that I got for fgets() != NULL
in the loop. And I hoped that *after* the loop the last read data is
still valid.
Les messages affichés proviennent d'usenet.