Liste des Groupes | Revenir à cl c |
First; thanks Kaz and Andrey for the replies. - As so often answering
more than I asked or needed. :-)
The provided C standard quote answers my question. - Thanks!
On 09.02.2025 07:23, Andrey Tarasevich wrote:On Sat 2/8/2025 9:59 PM, Janis Papanagnou wrote: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).
>
But the man page does not say anything whether this is guaranteed;
it says: "Reading stops after an EOF or a newline.", but it says
nothing about [not] writing to or [not] resetting the buffer.
>
Is that simple construct safe to get the last line of a text file?
What situation exactly are you talking about? When end-of-file is
encountered _immediately_, before reading the very first character?
Of when end-of-file is encountered after reading something (i.e.
when the last line in the file does not end with new-line
character)?
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
>
Les messages affichés proviennent d'usenet.