Deutsch   English   Français   Italiano  
<voe08n$1dstm$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: Mark Bourne <nntp.mbourne@spamgourmet.com>
Newsgroups: comp.lang.c
Subject: Re: Buffer contents well-defined after fgets() reaches EOF ?
Date: Mon, 10 Feb 2025 22:57:25 +0000
Organization: A noiseless patient Spider
Lines: 37
Message-ID: <voe08n$1dstm$1@dont-email.me>
References: <vo9g74$fu8u$1@dont-email.me> <vo9hlo$g0to$1@dont-email.me>
 <vo9ki6$gib5$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Mon, 10 Feb 2025 23:57:27 +0100 (CET)
Injection-Info: dont-email.me; posting-host="189876ba285a5019f1688dfef7a2745b";
	logging-data="1504182"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX19ev3CYBrzZaPJf2EdY52rW"
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:128.0) Gecko/20100101
 SeaMonkey/2.53.20
Cancel-Lock: sha1:z/ddSjOzGUm+PO3q2Oc+pZKGCv4=
In-Reply-To: <vo9ki6$gib5$1@dont-email.me>
Bytes: 2542

Janis Papanagnou wrote:
> I have a _coherent_ file, with a few NL terminated lines of text.
> 
> Usually I use fgets() in contexts where I process every line, like
> 
>      while (fgets (buf, BUFSIZ, fd) != NULL) {
>          operate_on (buf);
>      }
>      // here the status of buf[] is usually not important any more
> 
> My actual context was different, like
> 
>      while (fgets (buf, BUFSIZ, fd) != NULL) {
>          // buf[] contents are ignored here
>      }
>      operate_on (buf[]);  // which I assumed contains last line

....

> Usually I read and process the data that I got in buf from fgets()
> while there *is* data (fgets() != NULL), and I thus don't care any
> more about buffer contents validity after the loop (fgets() == NULL).
> 
> But now I wanted to ignore all data that I got for fgets() != NULL
> in the loop. And I hoped that *after* the loop the last read data is
> still valid.

What does fgets do if the file is completely empty?  I may be wrong 
(more familiar with Python than C these days), but it doesn't look like 
that should be any different from any other end-of-file condition, so 
presumably the first call to fgets would return NULL, without ever 
modifying the buffer.  Unless the buffer is initialised (e.g. to an 
empty string) before the while loop, that would result in an 
uninitialised buffer being passed to operate_on.

-- 
Mark.