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 : noone (at) *nospam* noone.net (Andrey Tarasevich)
Groupes : comp.lang.c
Date : 09. Feb 2025, 07:23:49
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <vo9hlo$g0to$1@dont-email.me>
References : 1
User-Agent : Mozilla Thunderbird
On Sat 2/8/2025 9:59 PM, Janis Papanagnou wrote:
To get the last line of a text file I'm using
      char buf[BUFSIZ];
     while (fgets (buf, BUFSIZ, fd) != NULL)
         ;   // read to last line
 If the end of the file is reached my test shows that the previous
contents of 'buf' are still existing (not cleared or overwritten).
 But the man page does not say anything whether this is guaranteed;
it says: "Reading stops after an EOF or a newline.", but it says
nothing about [not] writing to or [not] resetting the buffer.
 Is that simple construct safe to get the last line of a text file?
 
What situation exactly are you talking about? When end-of-file is encountered _immediately_, before reading the very first character? Of when end-of-file is encountered after reading something (i.e. when the last line in the file does not end with new-line character)?
The former situation is covered by the spec: "If end-of-file is encountered and no characters have been read into the array, the contents of the array remain unchanged and a null pointer is returned".
The second situation does not need additional clarifications. Per general spec as many characters as available before the end-of-file will be read and then terminated with '\0'. In such case there will be no new-line character in the buffer.
So, in both cases we are perfectly safe when reading the last line of a text file, if you don't forget to check the return value of `fgets`.
(This is all under assumption that size limit does not kick in. I believe your question is not about that.)
Note also that `fgets` is not permitted to assume that the limit value (the second parameter) correctly describes the accessible size of the buffer. E.g. for this reason it is not permitted to zero-out the buffer before reading. For example, this code is valid and has defined behavior
   char buffer[10];
   fgets(buffer, 1000, f);
provided the current line of the file fits into `char[10]`. I.e. even though we "lied" to `fgets` about the limit, it is still required to work correctly if the actual data fits into the actual buffer.
So, why do you care that "the previous contents of 'buf' are still existing"?
--
Best regards,
Andrey

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