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


Groups > comp.lang.c > #390313

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

From Tim Rentsch <tr.17687@z991.linuxsc.com>
Newsgroups comp.lang.c
Subject Re: Buffer contents well-defined after fgets() reaches EOF ?
Date 2025-02-13 07:29 -0800
Organization A noiseless patient Spider
Message-ID <86h64xzwuv.fsf@linuxsc.com> (permalink)
References (6 earlier) <vobu9b$12bi8$1@dont-email.me> <voc9d7$13pam$2@dont-email.me> <vodi7t$1b6at$1@dont-email.me> <voe7kp$1f0us$3@dont-email.me> <voeqtf$1liu1$1@dont-email.me>

Show all headers | View raw


Andrey Tarasevich <noone@noone.net> writes:

> On Mon 2/10/2025 5:03 PM, Lawrence D'Oliveiro wrote:
>
>> On Mon, 10 Feb 2025 13:58:05 -0500, James Kuyper wrote:
>>
>>> I don't see the contradiction.  If "no characters are read into the
>>> array", there is no such thing as "the last byte read into the array",
>>> so a null byte has no location where it should be written.  Therefore,
>>> there's no reason for changing the contents of the array.
>>
>> What if the array is only big enough for one byte?  In this case, no
>> characters can be read into it.  Is a trailing null inserted in this case?
>
> If by that you mean, "what if the value of 1 is passed as second
> argument", then, as I stated in one of my previous messages:
>
> No attempt to read anything from the stream is made, which means that
> end-of-file or I/O error conditions do not arise (unless, perhaps, the
> stream was already in error condition) and the [0] byte of the buffer
> is simply set to '\0'.

You're in good company.  Doing a web search turned up this description --

fgets() - Read a String from a Stream
Last Updated: 2024-09-20

    #include <stdio.h>
    char *fgets(char *string, int n, FILE *stream);

General Description

    Reads bytes from a stream pointed to by stream into an array
    pointed to by string, starting at the position indicated by the
    file position indicator.  Reading continues until the number of
    characters read is equal to n-1, or until a new-line character
    (\n), or until the end of the stream, whichever comes first.  The
    fgets() function stores the result in string and adds a null
    character (\0) to the end of the string.  The string includes the
    new-line character, if read.

    The fgets() function is not supported for files opened with
    type=record.

    The fgets() function has the same restriction as any read
    operation for a read immediately following a write or a write
    immediately following a read. Between a write and a subsequent
    read, an intervening flush or reposition must occur. Between a
    read and a subsequent write, an intervening flush or reposition
    must also occur, unless an EOF has been reached.

Returned Value

    If successful, fgets() returns a pointer to the string buffer.

    If unsuccessful, fgets() returns NULL.

    If n is less than or equal to 0, it indicates a domain error;
    errno is set to EDOM to indicate the cause of the failure.

    When n equals 1, it indicates a valid result.  It means that the
    string buffer has only room for the null terminator;  nothing is
    physically read from the file.  (Such an operation is still
    considered a read operation, so it cannot immediately follow a
    write operation unless an intervening flush or reposition
    operation occurs first.)

    If n is greater than 1, fgets() will only fail if an I/O error
    occurs or if EOF is reached and no data is read from the file.

    Note:  You should use ferror() and feof() to determine whether an
    error or an EOF condition occurred.  An EOF is only reached when
    an attempt is made to read "past" the last byte of data.  Reading
    up to and including the last byte of data does not turn on the EOF
    indicator.

    If EOF is reached after data has already been read into the string
    buffer, fgets() returns a pointer to the string buffer to indicate
    success. A subsequent call would return NULL since fgets() would
    reach EOF without reading any data.


That description was found on this page:

    https://www.ibm.com/docs/en/zvm/7.4?
        topic=descriptions-fgets-read-string-from-stream

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