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

Liste des GroupesRevenir à cl c 
Sujet : Re: Buffer contents well-defined after fgets() reaches EOF ?
De : Keith.S.Thompson+u (at) *nospam* gmail.com (Keith Thompson)
Groupes : comp.lang.c
Date : 10. Feb 2025, 05:37:34
Autres entêtes
Organisation : None to speak of
Message-ID : <87lduepg8h.fsf@nosuchdomain.example.com>
References : 1 2 3 4 5 6 7
User-Agent : Gnus/5.13 (Gnus v5.13)
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 */

Date Sujet#  Auteur
9 Feb 25 * Buffer contents well-defined after fgets() reaches EOF ?83Janis Papanagnou
9 Feb 25 +- Re: Buffer contents well-defined after fgets() reaches EOF ?1Kaz Kylheku
9 Feb 25 +* Re: Buffer contents well-defined after fgets() reaches EOF ?79Andrey Tarasevich
9 Feb 25 i+* Re: Buffer contents well-defined after fgets() reaches EOF ?61Andrey Tarasevich
10 Feb 25 ii`* Re: Buffer contents well-defined after fgets() reaches EOF ?60Lawrence D'Oliveiro
10 Feb 25 ii `* Re: Buffer contents well-defined after fgets() reaches EOF ?59Andrey Tarasevich
10 Feb 25 ii  +* Re: Buffer contents well-defined after fgets() reaches EOF ?37Andrey Tarasevich
10 Feb 25 ii  i+* Re: Buffer contents well-defined after fgets() reaches EOF ?35Michael S
13 Feb 25 ii  ii`* Re: Buffer contents well-defined after fgets() reaches EOF ?34Tim Rentsch
14 Feb 25 ii  ii `* Re: Buffer contents well-defined after fgets() reaches EOF ?33Michael S
14 Feb 25 ii  ii  +* Re: Buffer contents well-defined after fgets() reaches EOF ?2Michael S
14 Feb 25 ii  ii  i`- Re: Buffer contents well-defined after fgets() reaches EOF ?1Kaz Kylheku
14 Feb 25 ii  ii  +* Re: Buffer contents well-defined after fgets() reaches EOF ?21Kaz Kylheku
14 Feb 25 ii  ii  i+* Re: Buffer contents well-defined after fgets() reaches EOF ?3Keith Thompson
14 Feb 25 ii  ii  ii`* Re: Buffer contents well-defined after fgets() reaches EOF ?2Kaz Kylheku
15 Feb 25 ii  ii  ii `- Re: Buffer contents well-defined after fgets() reaches EOF ?1Michael S
14 Feb 25 ii  ii  i+* Re: Buffer contents well-defined after fgets() reaches EOF ?14Janis Papanagnou
15 Feb 25 ii  ii  ii+- Re: Buffer contents well-defined after fgets() reaches EOF ?1Michael S
15 Feb 25 ii  ii  ii`* Re: Buffer contents well-defined after fgets() reaches EOF ?12Michael S
16 Feb 25 ii  ii  ii +* Re: Buffer contents well-defined after fgets() reaches EOF ?7Janis Papanagnou
16 Feb 25 ii  ii  ii i+* Re: Buffer contents well-defined after fgets() reaches EOF ?3James Kuyper
16 Feb 25 ii  ii  ii ii+- Re: Buffer contents well-defined after fgets() reaches EOF ?1Kaz Kylheku
16 Feb 25 ii  ii  ii ii`- Re: Buffer contents well-defined after fgets() reaches EOF ?1Janis Papanagnou
16 Feb 25 ii  ii  ii i`* Re: Buffer contents well-defined after fgets() reaches EOF ?3Michael S
16 Feb 25 ii  ii  ii i `* Re: Buffer contents well-defined after fgets() reaches EOF ?2Janis Papanagnou
17 Feb 25 ii  ii  ii i  `- Re: Buffer contents well-defined after fgets() reaches EOF ?1Michael S
16 Feb 25 ii  ii  ii `* Re: Buffer contents well-defined after fgets() reaches EOF ?4Kaz Kylheku
16 Feb 25 ii  ii  ii  +* Re: Buffer contents well-defined after fgets() reaches EOF ?2Michael S
16 Feb 25 ii  ii  ii  i`- Re: Buffer contents well-defined after fgets() reaches EOF ?1Janis Papanagnou
16 Feb 25 ii  ii  ii  `- Re: Buffer contents well-defined after fgets() reaches EOF ?1Janis Papanagnou
15 Feb 25 ii  ii  i+- Re: Buffer contents well-defined after fgets() reaches EOF ?1Michael S
15 Feb 25 ii  ii  i`* Re: Buffer contents well-defined after fgets() reaches EOF ?2Michael S
16 Feb 25 ii  ii  i `- Re: Buffer contents well-defined after fgets() reaches EOF ?1Janis Papanagnou
14 Feb 25 ii  ii  +* Re: Buffer contents well-defined after fgets() reaches EOF ?5Janis Papanagnou
14 Feb 25 ii  ii  i`* Re: Buffer contents well-defined after fgets() reaches EOF ?4James Kuyper
14 Feb 25 ii  ii  i `* Re: Buffer contents well-defined after fgets() reaches EOF ?3Janis Papanagnou
15 Feb 25 ii  ii  i  `* Re: Buffer contents well-defined after fgets() reaches EOF ?2Michael S
16 Feb 25 ii  ii  i   `- Re: Buffer contents well-defined after fgets() reaches EOF ?1Janis Papanagnou
15 Feb 25 ii  ii  +* Re: Buffer contents well-defined after fgets() reaches EOF ?3Tim Rentsch
15 Feb 25 ii  ii  i`* Re: Buffer contents well-defined after fgets() reaches EOF ?2Michael S
19 Feb 25 ii  ii  i `- Re: Buffer contents well-defined after fgets() reaches EOF ?1Tim Rentsch
21 Feb 25 ii  ii  `- Re: Buffer contents well-defined after fgets() reaches EOF ?1Lawrence D'Oliveiro
15 Feb 25 ii  i`- Re: Buffer contents well-defined after fgets() reaches EOF ?1Tim Rentsch
10 Feb 25 ii  +- Re: Buffer contents well-defined after fgets() reaches EOF ?1Janis Papanagnou
10 Feb 25 ii  `* Re: Buffer contents well-defined after fgets() reaches EOF ?20Lawrence D'Oliveiro
10 Feb 25 ii   `* Re: Buffer contents well-defined after fgets() reaches EOF ?19Andrey Tarasevich
10 Feb 25 ii    `* Re: Buffer contents well-defined after fgets() reaches EOF ?18Lawrence D'Oliveiro
10 Feb 25 ii     `* Re: Buffer contents well-defined after fgets() reaches EOF ?17James Kuyper
11 Feb 25 ii      `* Re: Buffer contents well-defined after fgets() reaches EOF ?16Lawrence D'Oliveiro
11 Feb 25 ii       +* Re: Buffer contents well-defined after fgets() reaches EOF ?11James Kuyper
11 Feb 25 ii       i+- Re: Buffer contents well-defined after fgets() reaches EOF ?1Kaz Kylheku
11 Feb 25 ii       i`* Re: Buffer contents well-defined after fgets() reaches EOF ?9Lawrence D'Oliveiro
11 Feb 25 ii       i `* Re: Buffer contents well-defined after fgets() reaches EOF ?8James Kuyper
11 Feb 25 ii       i  +* Re: Buffer contents well-defined after fgets() reaches EOF ?3Lawrence D'Oliveiro
11 Feb 25 ii       i  i`* Re: Buffer contents well-defined after fgets() reaches EOF ?2James Kuyper
12 Feb 25 ii       i  i `- Re: Buffer contents well-defined after fgets() reaches EOF ?1Lawrence D'Oliveiro
11 Feb 25 ii       i  `* Re: Buffer contents well-defined after fgets() reaches EOF ?4Keith Thompson
11 Feb 25 ii       i   `* Re: Buffer contents well-defined after fgets() reaches EOF ?3James Kuyper
12 Feb 25 ii       i    `* Re: Buffer contents well-defined after fgets() reaches EOF ?2Keith Thompson
18 Feb 25 ii       i     `- Re: Buffer contents well-defined after fgets() reaches EOF ?1Tim Rentsch
11 Feb 25 ii       `* Re: Buffer contents well-defined after fgets() reaches EOF ?4Andrey Tarasevich
11 Feb 25 ii        +- Re: Buffer contents well-defined after fgets() reaches EOF ?1Andrey Tarasevich
11 Feb 25 ii        +- Re: Buffer contents well-defined after fgets() reaches EOF ?1Kaz Kylheku
13 Feb 25 ii        `- Re: Buffer contents well-defined after fgets() reaches EOF ?1Tim Rentsch
9 Feb 25 i`* Re: Buffer contents well-defined after fgets() reaches EOF ?17Janis Papanagnou
9 Feb 25 i +* Re: Buffer contents well-defined after fgets() reaches EOF ?3Michael S
9 Feb 25 i i`* Re: Buffer contents well-defined after fgets() reaches EOF ?2Janis Papanagnou
10 Feb 25 i i `- Re: Buffer contents well-defined after fgets() reaches EOF ?1Mark Bourne
9 Feb 25 i +* Re: Buffer contents well-defined after fgets() reaches EOF ?11Andrey Tarasevich
9 Feb 25 i i`* Re: Buffer contents well-defined after fgets() reaches EOF ?10Janis Papanagnou
10 Feb 25 i i `* Re: Buffer contents well-defined after fgets() reaches EOF ?9Keith Thompson
10 Feb 25 i i  `* Re: Buffer contents well-defined after fgets() reaches EOF ?8Janis Papanagnou
10 Feb 25 i i   `* Re: Buffer contents well-defined after fgets() reaches EOF ?7Keith Thompson
10 Feb 25 i i    `* Re: Buffer contents well-defined after fgets() reaches EOF ?6Janis Papanagnou
10 Feb 25 i i     `* Re: Buffer contents well-defined after fgets() reaches EOF ?5Keith Thompson
10 Feb 25 i i      +* Re: Buffer contents well-defined after fgets() reaches EOF ?3Janis Papanagnou
11 Feb 25 i i      i+- Re: Buffer contents well-defined after fgets() reaches EOF ?1Mark Bourne
11 Feb 25 i i      i`- Re: Buffer contents well-defined after fgets() reaches EOF ?1Kaz Kylheku
17 Feb 25 i i      `- Re: Buffer contents well-defined after fgets() reaches EOF ?1Lawrence D'Oliveiro
10 Feb 25 i `* Re: Buffer contents well-defined after fgets() reaches EOF ?2Mark Bourne
11 Feb 25 i  `- Re: Buffer contents well-defined after fgets() reaches EOF ?1Mark Bourne
10 Feb 25 `* Re: Buffer contents well-defined after fgets() reaches EOF ?2Ben Bacarisse
10 Feb 25  `- Re: Buffer contents well-defined after fgets() reaches EOF ?1Janis Papanagnou

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal