Sujet : Re: Buffer contents well-defined after fgets() reaches EOF ?
De : noone (at) *nospam* noone.net (Andrey Tarasevich)
Groupes : comp.lang.cDate : 10. Feb 2025, 05:11:22
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <vobu9b$12bi8$1@dont-email.me>
References : 1 2 3 4 5 6
User-Agent : Mozilla Thunderbird
On Sun 2/9/2025 7:28 PM, Lawrence D'Oliveiro wrote:
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.
Well, the language standard says differently.
You are referring to a specific manpage that follows POSIX. When taken literally it seems to contradict the standard specification for `fgets`, but I highly doubt this was the intent. Apparently someone tried to re-word the spec for better readability, but managed to botch it.
This manpage, for one example, is in full agreement with the standard
https://www.man7.org/linux/man-pages/man3/fgets.3p.htmlA practical experiment demonstrates that [supposedly] POSIX-obeying implementations do not write '\0' into the buffer in "immediate end-of-file" situations:
https://coliru.stacked-crooked.com/a/3e672e6718dd388b-- Best regards,Andrey