Liste des Groupes | Revenir à cl c |
Janis Papanagnou <janis_papanagnou+ng@hotmail.com> writes:
To get the last line of a text file I'm using
>
char buf[BUFSIZ];
while (fgets (buf, BUFSIZ, fd) != NULL)
; // read to last line
>
If the end of the file is reached my test shows that the previous
contents of 'buf' are still existing (not cleared or overwritten).
Something that has not yet come up (as far as I can see) is that you
might need to handle an empty file. In such a case, nothing gets
written and fgets returns NULL right away. Processing buf in this
situation is then undefined.
One way to handle this is to put into buf something that can't get read
by fgets. Two newlines is a good candidate:
char buf[BUFSIZE] = "\n\n";
You can then test for that if need be, though of course it all depends
on what your application is doing.
Les messages affichés proviennent d'usenet.