Warning: mysqli::__construct(): (HY000/1203): User howardkn already has more than 'max_user_connections' active connections in D:\Inetpub\vhosts\howardknight.net\al.howardknight.net\includes\artfuncs.php on line 21
Failed to connect to MySQL: (1203) User howardkn already has more than 'max_user_connections' active connections
Warning: mysqli::query(): Couldn't fetch mysqli in D:\Inetpub\vhosts\howardknight.net\al.howardknight.net\index.php on line 66
Article <vo9hlo$g0to$1@dont-email.me>
Deutsch   English   Français   Italiano  
<vo9hlo$g0to$1@dont-email.me>

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: Andrey Tarasevich <noone@noone.net>
Newsgroups: comp.lang.c
Subject: Re: Buffer contents well-defined after fgets() reaches EOF ?
Date: Sat, 8 Feb 2025 22:23:49 -0800
Organization: A noiseless patient Spider
Lines: 55
Message-ID: <vo9hlo$g0to$1@dont-email.me>
References: <vo9g74$fu8u$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sun, 09 Feb 2025 07:23:58 +0100 (CET)
Injection-Info: dont-email.me; posting-host="b46cc674ba558476b6b694ab42c1b417";
	logging-data="525240"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX18VkL2uhsTWBXWw2HiHJ3hs"
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:r/UoeF+Otv37SAtrEuL2vKfYr/g=
In-Reply-To: <vo9g74$fu8u$1@dont-email.me>
Content-Language: en-US
Bytes: 3320

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)?

The former situation is covered by the spec: "If end-of-file is 
encountered and no characters have been read into the array, the 
contents of the array remain unchanged and a null pointer is returned".

The second situation does not need additional clarifications. Per 
general spec as many characters as available before the end-of-file will 
be read and then terminated with '\0'. In such case there will be no 
new-line character in the buffer.

So, in both cases we are perfectly safe when reading the last line of a 
text file, if you don't forget to check the return value of `fgets`.

(This is all under assumption that size limit does not kick in. I 
believe your question is not about that.)

Note also that `fgets` is not permitted to assume that the limit value 
(the second parameter) correctly describes the accessible size of the 
buffer. E.g. for this reason it is not permitted to zero-out the buffer 
before reading. For example, this code is valid and has defined behavior

   char buffer[10];
   fgets(buffer, 1000, f);

provided the current line of the file fits into `char[10]`. I.e. even 
though we "lied" to `fgets` about the limit, it is still required to 
work correctly if the actual data fits into the actual buffer.

So, why do you care that "the previous contents of 'buf' are still 
existing"?

-- 
Best regards,
Andrey