Sujet : Re: Buffer contents well-defined after fgets() reaches EOF ?
De : tr.17687 (at) *nospam* z991.linuxsc.com (Tim Rentsch)
Groupes : comp.lang.cDate : 13. Feb 2025, 16:29:44
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <86h64xzwuv.fsf@linuxsc.com>
References : 1 2 3 4 5 6 7 8 9 10 11
User-Agent : Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
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