From: "Charlie Gordon" <gmane@chqrlie.org>
To: linux-c-programming@vger.kernel.org
Subject: Re: feof fails
Date: Wed, 15 Sep 2004 16:23:48 +0200 [thread overview]
Message-ID: <ci9j8e$v56$1@sea.gmane.org> (raw)
In-Reply-To: B0003441729@hermes.imsd.uni-mainz.de
"Christian Stalp" <stalp@imbei.uni-mainz.de> wrote in message
news:B0003441729@hermes.imsd.uni-mainz.de...
Hello together,
I have a simple problem. I opened a file with fopen and read from the file
with:
while ( !feof( infile ) )
{
fgets( buffer, BUFFER_SIZE, infile );
new_number = atoi ( buffer );
.....
The end of file condition is detected when trying to read from the file and
failing.
Thus feof(infile) doesn't return non zero until after fgets fails at least
once.
You should test fgets return value to detect end of file.
The reason the last number is read twice is that your particular
implementation of fgets doesn't modify buffer upon end of file, it just
returns NULL, which you don't check before parsing the contents of the
buffer.
try this:
while (fgets(buffer, BUFFER_SIZE, infile)) {
new_number = atoi(buffer);
...
}
simpler and better !
Chqrlie.
PS: wether the file is terminated with a newline or not is irrelevant to
this question.
next prev parent reply other threads:[~2004-09-15 14:23 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2004-09-15 9:39 feof fails Christian Stalp
2004-09-15 11:13 ` Pankaj
2004-09-15 14:23 ` Charlie Gordon [this message]
-- strict thread matches above, loose matches on Subject: below --
2004-09-15 14:57 Huber, George K RDECOM CERDEC STCD SRI
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to='ci9j8e$v56$1@sea.gmane.org' \
--to=gmane@chqrlie.org \
--cc=linux-c-programming@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).