Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #390256

Re: Buffer contents well-defined after fgets() reaches EOF ?

From Keith Thompson <Keith.S.Thompson+u@gmail.com>
Newsgroups comp.lang.c
Subject Re: Buffer contents well-defined after fgets() reaches EOF ?
Date 2025-02-09 16:57 -0800
Organization None to speak of
Message-ID <87y0yepqg1.fsf@nosuchdomain.example.com> (permalink)
References <vo9g74$fu8u$1@dont-email.me> <vo9hlo$g0to$1@dont-email.me> <vo9ki6$gib5$1@dont-email.me> <voahgv$mdud$2@dont-email.me> <voaovv$ocot$1@dont-email.me>

Show all headers | View raw


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.com
void Void(void) { Void(); } /* The recursive call of the void */

Back to comp.lang.c | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

Buffer contents well-defined after fgets() reaches EOF ? Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2025-02-09 06:59 +0100
  Re: Buffer contents well-defined after fgets() reaches EOF ? Kaz Kylheku <643-408-1753@kylheku.com> - 2025-02-09 06:23 +0000
  Re: Buffer contents well-defined after fgets() reaches EOF ? Andrey Tarasevich <noone@noone.net> - 2025-02-08 22:23 -0800
    Re: Buffer contents well-defined after fgets() reaches EOF ? Andrey Tarasevich <noone@noone.net> - 2025-02-08 23:12 -0800
      Re: Buffer contents well-defined after fgets() reaches EOF ? Lawrence D'Oliveiro <ldo@nz.invalid> - 2025-02-09 23:52 +0000
        Re: Buffer contents well-defined after fgets() reaches EOF ? Andrey Tarasevich <noone@noone.net> - 2025-02-09 17:06 -0800
          Re: Buffer contents well-defined after fgets() reaches EOF ? Andrey Tarasevich <noone@noone.net> - 2025-02-09 17:22 -0800
            Re: Buffer contents well-defined after fgets() reaches EOF ? Michael S <already5chosen@yahoo.com> - 2025-02-10 12:49 +0200
              Re: Buffer contents well-defined after fgets() reaches EOF ? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2025-02-13 07:14 -0800
                Re: Buffer contents well-defined after fgets() reaches EOF ? Michael S <already5chosen@yahoo.com> - 2025-02-14 16:51 +0200
                Re: Buffer contents well-defined after fgets() reaches EOF ? scott@slp53.sl.home (Scott Lurndal) - 2025-02-14 15:10 +0000
                Re: Buffer contents well-defined after fgets() reaches EOF ? Michael S <already5chosen@yahoo.com> - 2025-02-14 17:23 +0200
                Re: Buffer contents well-defined after fgets() reaches EOF ? scott@slp53.sl.home (Scott Lurndal) - 2025-02-14 16:46 +0000
                Re: Buffer contents well-defined after fgets() reaches EOF ? Kaz Kylheku <643-408-1753@kylheku.com> - 2025-02-14 17:28 +0000
                Re: Buffer contents well-defined after fgets() reaches EOF ? Kaz Kylheku <643-408-1753@kylheku.com> - 2025-02-14 17:22 +0000
                Re: Buffer contents well-defined after fgets() reaches EOF ? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2025-02-14 11:03 -0800
                Re: Buffer contents well-defined after fgets() reaches EOF ? Kaz Kylheku <643-408-1753@kylheku.com> - 2025-02-14 19:34 +0000
                Re: Buffer contents well-defined after fgets() reaches EOF ? Michael S <already5chosen@yahoo.com> - 2025-02-15 20:06 +0200
                Re: Buffer contents well-defined after fgets() reaches EOF ? scott@slp53.sl.home (Scott Lurndal) - 2025-02-14 20:01 +0000
                Re: Buffer contents well-defined after fgets() reaches EOF ? Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2025-02-14 20:51 +0100
                Re: Buffer contents well-defined after fgets() reaches EOF ? Michael S <already5chosen@yahoo.com> - 2025-02-15 19:02 +0200
                Re: Buffer contents well-defined after fgets() reaches EOF ? Michael S <already5chosen@yahoo.com> - 2025-02-15 19:29 +0200
                Re: Buffer contents well-defined after fgets() reaches EOF ? Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2025-02-16 04:29 +0100
                Re: Buffer contents well-defined after fgets() reaches EOF ? James Kuyper <jameskuyper@alumni.caltech.edu> - 2025-02-16 01:04 -0500
                Re: Buffer contents well-defined after fgets() reaches EOF ? Kaz Kylheku <643-408-1753@kylheku.com> - 2025-02-16 07:37 +0000
                Re: Buffer contents well-defined after fgets() reaches EOF ? Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2025-02-16 18:59 +0100
                Re: Buffer contents well-defined after fgets() reaches EOF ? Michael S <already5chosen@yahoo.com> - 2025-02-16 10:48 +0200
                Re: Buffer contents well-defined after fgets() reaches EOF ? Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2025-02-16 19:14 +0100
                Re: Buffer contents well-defined after fgets() reaches EOF ? Michael S <already5chosen@yahoo.com> - 2025-02-17 11:54 +0200
                Re: Buffer contents well-defined after fgets() reaches EOF ? Kaz Kylheku <643-408-1753@kylheku.com> - 2025-02-16 07:32 +0000
                Re: Buffer contents well-defined after fgets() reaches EOF ? Michael S <already5chosen@yahoo.com> - 2025-02-16 11:05 +0200
                Re: Buffer contents well-defined after fgets() reaches EOF ? Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2025-02-16 19:25 +0100
                Re: Buffer contents well-defined after fgets() reaches EOF ? Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2025-02-16 19:21 +0100
                Re: Buffer contents well-defined after fgets() reaches EOF ? scott@slp53.sl.home (Scott Lurndal) - 2025-02-16 20:26 +0000
                Re: Buffer contents well-defined after fgets() reaches EOF ? Michael S <already5chosen@yahoo.com> - 2025-02-15 19:41 +0200
                Re: Buffer contents well-defined after fgets() reaches EOF ? Michael S <already5chosen@yahoo.com> - 2025-02-15 20:29 +0200
                Re: Buffer contents well-defined after fgets() reaches EOF ? Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2025-02-16 04:33 +0100
                Re: Buffer contents well-defined after fgets() reaches EOF ? Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2025-02-14 20:23 +0100
                Re: Buffer contents well-defined after fgets() reaches EOF ? James Kuyper <jameskuyper@alumni.caltech.edu> - 2025-02-14 14:38 -0500
                Re: Buffer contents well-defined after fgets() reaches EOF ? Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2025-02-14 21:02 +0100
                Re: Buffer contents well-defined after fgets() reaches EOF ? Michael S <already5chosen@yahoo.com> - 2025-02-15 19:53 +0200
                Re: Buffer contents well-defined after fgets() reaches EOF ? Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2025-02-16 04:48 +0100
                Re: Buffer contents well-defined after fgets() reaches EOF ? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2025-02-15 08:37 -0800
                Re: Buffer contents well-defined after fgets() reaches EOF ? Michael S <already5chosen@yahoo.com> - 2025-02-15 20:08 +0200
                Re: Buffer contents well-defined after fgets() reaches EOF ? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2025-02-18 20:17 -0800
                Re: Buffer contents well-defined after fgets() reaches EOF ? Lawrence D'Oliveiro <ldo@nz.invalid> - 2025-02-21 05:58 +0000
            Re: Buffer contents well-defined after fgets() reaches EOF ? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2025-02-15 08:12 -0800
          Re: Buffer contents well-defined after fgets() reaches EOF ? Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2025-02-10 02:44 +0100
          Re: Buffer contents well-defined after fgets() reaches EOF ? Lawrence D'Oliveiro <ldo@nz.invalid> - 2025-02-10 03:28 +0000
            Re: Buffer contents well-defined after fgets() reaches EOF ? Andrey Tarasevich <noone@noone.net> - 2025-02-09 20:11 -0800
              Re: Buffer contents well-defined after fgets() reaches EOF ? Lawrence D'Oliveiro <ldo@nz.invalid> - 2025-02-10 07:21 +0000
                Re: Buffer contents well-defined after fgets() reaches EOF ? scott@slp53.sl.home (Scott Lurndal) - 2025-02-10 16:39 +0000
                Re: Buffer contents well-defined after fgets() reaches EOF ? James Kuyper <jameskuyper@alumni.caltech.edu> - 2025-02-10 13:58 -0500
                Re: Buffer contents well-defined after fgets() reaches EOF ? Lawrence D'Oliveiro <ldo@nz.invalid> - 2025-02-11 01:03 +0000
                Re: Buffer contents well-defined after fgets() reaches EOF ? James Kuyper <jameskuyper@alumni.caltech.edu> - 2025-02-10 22:33 -0500
                Re: Buffer contents well-defined after fgets() reaches EOF ? Kaz Kylheku <643-408-1753@kylheku.com> - 2025-02-11 03:42 +0000
                Re: Buffer contents well-defined after fgets() reaches EOF ? Lawrence D'Oliveiro <ldo@nz.invalid> - 2025-02-11 04:54 +0000
                Re: Buffer contents well-defined after fgets() reaches EOF ? James Kuyper <jameskuyper@alumni.caltech.edu> - 2025-02-11 13:07 -0500
                Re: Buffer contents well-defined after fgets() reaches EOF ? Lawrence D'Oliveiro <ldo@nz.invalid> - 2025-02-11 21:47 +0000
                Re: Buffer contents well-defined after fgets() reaches EOF ? James Kuyper <jameskuyper@alumni.caltech.edu> - 2025-02-11 17:44 -0500
                Re: Buffer contents well-defined after fgets() reaches EOF ? Lawrence D'Oliveiro <ldo@nz.invalid> - 2025-02-12 06:16 +0000
                Re: Buffer contents well-defined after fgets() reaches EOF ? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2025-02-11 13:59 -0800
                Re: Buffer contents well-defined after fgets() reaches EOF ? James Kuyper <jameskuyper@alumni.caltech.edu> - 2025-02-11 17:58 -0500
                Re: Buffer contents well-defined after fgets() reaches EOF ? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2025-02-11 15:40 -0800
                Re: Buffer contents well-defined after fgets() reaches EOF ? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2025-02-17 21:11 -0800
                Re: Buffer contents well-defined after fgets() reaches EOF ? Andrey Tarasevich <noone@noone.net> - 2025-02-10 22:32 -0800
                Re: Buffer contents well-defined after fgets() reaches EOF ? Andrey Tarasevich <noone@noone.net> - 2025-02-10 22:38 -0800
                Re: Buffer contents well-defined after fgets() reaches EOF ? Kaz Kylheku <643-408-1753@kylheku.com> - 2025-02-11 12:04 +0000
                Re: Buffer contents well-defined after fgets() reaches EOF ? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2025-02-13 07:29 -0800
    Re: Buffer contents well-defined after fgets() reaches EOF ? Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2025-02-09 08:13 +0100
      Re: Buffer contents well-defined after fgets() reaches EOF ? Michael S <already5chosen@yahoo.com> - 2025-02-09 12:50 +0200
        Re: Buffer contents well-defined after fgets() reaches EOF ? Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2025-02-09 18:29 +0100
          Re: Buffer contents well-defined after fgets() reaches EOF ? Mark Bourne <nntp.mbourne@spamgourmet.com> - 2025-02-10 21:57 +0000
      Re: Buffer contents well-defined after fgets() reaches EOF ? Andrey Tarasevich <noone@noone.net> - 2025-02-09 07:27 -0800
        Re: Buffer contents well-defined after fgets() reaches EOF ? Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2025-02-09 18:34 +0100
          Re: Buffer contents well-defined after fgets() reaches EOF ? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2025-02-09 16:57 -0800
            Re: Buffer contents well-defined after fgets() reaches EOF ? Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2025-02-10 02:35 +0100
              Re: Buffer contents well-defined after fgets() reaches EOF ? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2025-02-09 20:37 -0800
                Re: Buffer contents well-defined after fgets() reaches EOF ? Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2025-02-10 07:08 +0100
                Re: Buffer contents well-defined after fgets() reaches EOF ? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2025-02-09 22:41 -0800
                Re: Buffer contents well-defined after fgets() reaches EOF ? Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2025-02-10 07:54 +0100
                Re: Buffer contents well-defined after fgets() reaches EOF ? Mark Bourne <nntp.mbourne@spamgourmet.com> - 2025-02-10 23:22 +0000
                Re: Buffer contents well-defined after fgets() reaches EOF ? Kaz Kylheku <643-408-1753@kylheku.com> - 2025-02-11 00:59 +0000
                Re: Buffer contents well-defined after fgets() reaches EOF ? Lawrence D'Oliveiro <ldo@nz.invalid> - 2025-02-17 02:50 +0000
      Re: Buffer contents well-defined after fgets() reaches EOF ? Mark Bourne <nntp.mbourne@spamgourmet.com> - 2025-02-10 22:57 +0000
        Re: Buffer contents well-defined after fgets() reaches EOF ? Mark Bourne <nntp.mbourne@spamgourmet.com> - 2025-02-10 23:24 +0000
  Re: Buffer contents well-defined after fgets() reaches EOF ? Ben Bacarisse <ben@bsb.me.uk> - 2025-02-10 01:32 +0000
    Re: Buffer contents well-defined after fgets() reaches EOF ? Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2025-02-10 02:40 +0100

csiph-web