Path: ...!news.nobody.at!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c Subject: Re: Buffer contents well-defined after fgets() reaches EOF ? Date: Sun, 09 Feb 2025 16:57:02 -0800 Organization: None to speak of Lines: 48 Message-ID: <87y0yepqg1.fsf@nosuchdomain.example.com> References: MIME-Version: 1.0 Content-Type: text/plain Injection-Date: Mon, 10 Feb 2025 01:57:03 +0100 (CET) Injection-Info: dont-email.me; posting-host="f4acd3ba793eeb2facf0b27d62bf5cd0"; logging-data="945334"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/X5hCpJsy8blSYDR1sk5ri" User-Agent: Gnus/5.13 (Gnus v5.13) Cancel-Lock: sha1:1qSsYsxtSmu7JasvaiTHAsdmI0c= sha1:efXvpjA9TvyLxT0SP2P+ulp0Ddg= Bytes: 3204 Janis Papanagnou 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 >> >> ? >> >> 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 control > character then it's complete. (Similar with on old Apple/Macs and > 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.com void Void(void) { Void(); } /* The recursive call of the void */