Sujet : Re: Buffer contents well-defined after fgets() reaches EOF ?
De : ldo (at) *nospam* nz.invalid (Lawrence D'Oliveiro)
Groupes : comp.lang.cDate : 10. Feb 2025, 04:28:01
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <vobro1$u9fa$1@dont-email.me>
References : 1 2 3 4 5
User-Agent : Pan/0.161 (Chasiv Yar; )
On Sun, 9 Feb 2025 17:06:02 -0800, Andrey Tarasevich wrote:
On Sun 2/9/2025 3:52 PM, Lawrence D'Oliveiro wrote:
On Sat, 8 Feb 2025 23:12:44 -0800, Andrey Tarasevich wrote:
If `fgets` reads nothing (instant end-of-file), the entire buffer
remains untouched.
You mean, only a single null byte gets written.
No. The buffer is not changed at all in such case.
From the man page <
https://manpages.debian.org/fgets(3)>:
fgets() reads in at most one less than size characters from stream and
stores them into the buffer pointed to by s. Reading stops after an
EOF or a newline. If a newline is read, it is stored into the buffer.
A terminating null byte ('\0') is stored after the last character in
the buffer.
Note there is no qualification like “a terminating null byte is stored
after the last character if EOF was not reached”. It’s clear the
terminating null byte is *always* stored.