| Deutsch English Français Italiano |
|
<20250209125046.00001189@yahoo.com> View for Bookmarking (what is this?) Look up another Usenet article |
Path: ...!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!eternal-september.org!.POSTED!not-for-mail
From: Michael S <already5chosen@yahoo.com>
Newsgroups: comp.lang.c
Subject: Re: Buffer contents well-defined after fgets() reaches EOF ?
Date: Sun, 9 Feb 2025 12:50:46 +0200
Organization: A noiseless patient Spider
Lines: 58
Message-ID: <20250209125046.00001189@yahoo.com>
References: <vo9g74$fu8u$1@dont-email.me>
<vo9hlo$g0to$1@dont-email.me>
<vo9ki6$gib5$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=US-ASCII
Content-Transfer-Encoding: 7bit
Injection-Date: Sun, 09 Feb 2025 11:50:46 +0100 (CET)
Injection-Info: dont-email.me; posting-host="8e2d9f7cf4b47825ab2c863bbd44b8cd";
logging-data="566868"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18VIrgWNhT1UL3/2rC2ng40EPjWQEu2Exw="
Cancel-Lock: sha1:Y0WEqEvPkwMwtTU6EgNz8QcckEs=
X-Newsreader: Claws Mail 3.19.1 (GTK+ 2.24.33; x86_64-w64-mingw32)
Bytes: 3147
On Sun, 9 Feb 2025 08:13:10 +0100
Janis Papanagnou <janis_papanagnou+ng@hotmail.com> wrote:
> 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.
>
I wonder what you mean by "coherent".
> 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
>
It depends on definition of "last line".
What do you consider "last line" of the file in which last character is
not LF? The one before the last LF or one after? Your code would get
the latter.