Path: csiph.com!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!eternal-september.org!.POSTED!not-for-mail From: Tim Rentsch Newsgroups: comp.lang.c Subject: Re: Buffer contents well-defined after fgets() reaches EOF ? Date: Thu, 13 Feb 2025 07:29:44 -0800 Organization: A noiseless patient Spider Lines: 86 Message-ID: <86h64xzwuv.fsf@linuxsc.com> References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Date: Thu, 13 Feb 2025 16:29:45 +0100 (CET) Injection-Info: dont-email.me; posting-host="93acdcaaf30489276d8996fcd320a4b9"; logging-data="3112477"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+rODYrDkuA77SWnWOK2JG8sauFC6uFvaA=" User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux) Cancel-Lock: sha1:/vUgdTGHBCuRBD+4FaPmQKoBF9I= sha1:bHDIBj8EZi3m46ngvKqvNnwBe4w= Xref: csiph.com comp.lang.c:390313 Andrey Tarasevich 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 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