Sujet : Re: Buffer contents well-defined after fgets() reaches EOF ?
De : Keith.S.Thompson+u (at) *nospam* gmail.com (Keith Thompson)
Groupes : comp.lang.cDate : 10. Feb 2025, 01:57:02
Autres entêtes
Organisation : None to speak of
Message-ID : <87y0yepqg1.fsf@nosuchdomain.example.com>
References : 1 2 3 4 5
User-Agent : Gnus/5.13 (Gnus v5.13)
Janis Papanagnou <janis_papanagnou+
ng@hotmail.com> writes:
On 09.02.2025 16:27, Andrey Tarasevich wrote:
[...]
Say, what do you want to see as "the
last line" in a file that ends with
abracadabra\n<EOF here>
?
Is "abracadabra" the last line? Or is the last line supposed to be empty
in this case?
>
If "\n" is a string literal (2 characters, '\' and 'n') then it's an
incomplete line (as to my standards), if it's meant as a <LF> control
character then it's complete. (Similar with <CR> on old Apple/Macs and
<CR><LF> on DOS-alike systems.)
It seems obvious to me that Andrey intended the \n to be a new-line
character (which is almost always LF in modern C implementations).
Here's (some of) what the C standard says about text streams:
A text stream is an ordered sequence of characters composed into
lines, each line consisting of zero or more characters plus a
terminating new-line character. Whether the last line requires
a terminating new-line character is implementation-defined.
For an implementation that *doesn't* require a new-line on the
last line, a stream without a trailing new-line is valid. For an
implementation that *does* require it, such a stream is invalid,
and a program that attempts to process it can have undefined behavior.
Most modern implementations don't require that trailing new-line.
For example, `echo -n hello > hello.txt` creates a valid text file.
Of course a C program that deals with text files can impose any
additional restrictions its author likes.
The above describes how a text stream looks to a C program. The
external representation can be quite different, with transformations
to map between them. The most common such transformation is
mapping the DOS/Windows CR-LF line terminator to LF on input, and
vice versa on output. Or the external representation might store
each line as a fixed-length character sequence padded with spaces.
-- Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.comvoid Void(void) { Void(); } /* The recursive call of the void */