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


Groups > comp.lang.c > #390273

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 20:37 -0800
Organization None to speak of
Message-ID <87lduepg8h.fsf@nosuchdomain.example.com> (permalink)
References (2 earlier) <vo9ki6$gib5$1@dont-email.me> <voahgv$mdud$2@dont-email.me> <voaovv$ocot$1@dont-email.me> <87y0yepqg1.fsf@nosuchdomain.example.com> <vobl46$td52$1@dont-email.me>

Show all headers | View raw


Janis Papanagnou <janis_papanagnou+ng@hotmail.com> writes:
> On 10.02.2025 01:57, Keith Thompson wrote:
[...]
>> 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. 
>
> (Concerning this thread; I'm anyway operating on custom data files
> in plain text format, so I'm less concerned about how "C" compilers
> expect their "C" source.)

The requirements for text streams are distinct from the requirements
for C source files.  For example, you might have a cross-compiler
where C source files follow the rules of the OS where the compiler
runs, and text files processed via stdio follow the rules of
the target system.  And a C compiler might not use stdio to read
source files.  It might not even be implemented in C.

In particular, the standard has this specific requirement for source
files (this is from the "Translation phases" section):

    A source file that is not empty shall end in a new-line
    character, which shall not be immediately preceded by a backslash
    character before any such splicing takes place.

(This is in translation phase 2; any new-line characters might be
the result of a transformation during phase 1.)

So a non-empty file not ending in a new-line character might be a
valid text file for use with stdio, but is not a valid C source file.
On the other hand, the mapping described in translation phase 1
might add a new-line character to such a file, so a conforming
compiler could accept such a source file without complaint.

Of the compilers I've tried, gcc and tcc quietly accept a source
file with no trailing newline, and clang rejects it with the right
options (-std=c?? -pedantic-errors).

[...]

> The most extreme context I had worked in was a company that allowed
> (for every employee) a free choice of used computer technology; that
> led to program text files that literally had all the inconsistencies.
> Since many files were edited by different folks there where all sorts
> of line terminators mixed even in the same one file, and there either
> were complete last lines or not. The (some?) IDEs used were tolerant
> WRT line terminators and their mixing. Other tools reacted sensibly.
> The first thing I've done was to write a "C" tool to detect and fix
> these sorts of inconsistencies.

Been there, done that.  There seems to be a tendency in the Windows
world to create text files with no terminator on the last line.
In some cases I've been able to translate the source files to a
consistent format.  In others, doing so would have created huge
diffs in the source control system, so I left well enough alone.

My preferred editor, vim, handles files with either LF or CRLF line
endings gracefully, but if there's a mix it shows "^M" at the end of
each line that has a Windows-style CRLF ending.  I found a possible
solution, but I haven't bothered using it since I'm not currently
dealing with such files.

<https://vi.stackexchange.com/q/39297/2380>

This is already off-topic, so I won't even mention tabs vs. 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