| Deutsch English Français Italiano |
|
<vo9khf$ggd4$1@dont-email.me> View for Bookmarking (what is this?) Look up another Usenet article |
Path: ...!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!eternal-september.org!.POSTED!not-for-mail From: Andrey Tarasevich <noone@noone.net> Newsgroups: comp.lang.c Subject: Re: Buffer contents well-defined after fgets() reaches EOF ? Date: Sat, 8 Feb 2025 23:12:44 -0800 Organization: A noiseless patient Spider Lines: 27 Message-ID: <vo9khf$ggd4$1@dont-email.me> References: <vo9g74$fu8u$1@dont-email.me> <vo9hlo$g0to$1@dont-email.me> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Injection-Date: Sun, 09 Feb 2025 08:12:48 +0100 (CET) Injection-Info: dont-email.me; posting-host="b46cc674ba558476b6b694ab42c1b417"; logging-data="541092"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19Mhs0L2dWNyAhkvMpCX1lY" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:8at82+iNqBBNM99NpWUdSwF4mbE= Content-Language: en-US In-Reply-To: <vo9hlo$g0to$1@dont-email.me> Bytes: 2140 On Sat 2/8/2025 10:23 PM, Andrey Tarasevich wrote: > > 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. .... and this part of specification effectively guarantees, that any [tail] portion of the buffer not overwritten by the characters obtained from file, will remain unchanged. If `fgets` reads 5 characters from the file, only first 6 characters of the buffer will be overwritten, while the rest is guaranteed to remain untouched. If `fgets` reads nothing (instant end-of-file), the entire buffer remains untouched. -- Best regards, Andrey