* feof fails
@ 2004-09-15 9:39 Christian Stalp
2004-09-15 11:13 ` Pankaj
2004-09-15 14:23 ` Charlie Gordon
0 siblings, 2 replies; 4+ messages in thread
From: Christian Stalp @ 2004-09-15 9:39 UTC (permalink / raw)
To: linux-c-programming
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 file is filled withe a stack of numbers, 7 charakters and each is
terminated by an '\n'.
My problem now is, that the last number is been read twice. What is the
problem?
Gruss Christian
--
Christian Stalp
Institut für Medizinische Biometrie, Epidemiologie und Informatik (IMBEI)
Obere Zahlbacher Straße 69
55131 Mainz
Tel.: 06131/ 17-6852
E-Mail: stalp@imbei.uni-mainz.de
Internet: www.imbei.de
-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
^ permalink raw reply [flat|nested] 4+ messages in thread* Re: feof fails
2004-09-15 9:39 feof fails Christian Stalp
@ 2004-09-15 11:13 ` Pankaj
2004-09-15 14:23 ` Charlie Gordon
1 sibling, 0 replies; 4+ messages in thread
From: Pankaj @ 2004-09-15 11:13 UTC (permalink / raw)
To: stalp; +Cc: linux-c-programming
hi,
Check whether u have put a \n at the end of last character..
u might have missed it.
Pankaj
On Wednesday 15 Sep 2004 3:09 pm, Christian Stalp wrote:
> 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 file is filled withe a stack of numbers, 7 charakters and each is
> terminated by an '\n'.
>
> My problem now is, that the last number is been read twice. What is the
> problem?
>
> Gruss Christian
^ permalink raw reply [flat|nested] 4+ messages in thread* Re: feof fails
2004-09-15 9:39 feof fails Christian Stalp
2004-09-15 11:13 ` Pankaj
@ 2004-09-15 14:23 ` Charlie Gordon
1 sibling, 0 replies; 4+ messages in thread
From: Charlie Gordon @ 2004-09-15 14:23 UTC (permalink / raw)
To: linux-c-programming
"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.
^ permalink raw reply [flat|nested] 4+ messages in thread
* RE: feof fails
@ 2004-09-15 14:57 Huber, George K RDECOM CERDEC STCD SRI
0 siblings, 0 replies; 4+ messages in thread
From: Huber, George K RDECOM CERDEC STCD SRI @ 2004-09-15 14:57 UTC (permalink / raw)
To: linux-c-programming
Christian wrote:
>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 file is filled withe a stack of numbers, 7 charakters and each is
>terminated by an '\n'.
>My problem now is, that the last number is been read twice. What is the
>problem?
The problem is not with feof, but rather how input works (this issue plagued
me for a number of years). Lets assume that the last line of your file is
(where
^ represents the new-line `character', and the arrow is the current file
pointer):
1234567^
^
|
Now, when fgets process this line, it reads up to and including the the new
line
character, moving the file pointer thus:
1234567^
^
|
At this point, we have not hit the EOF `character' as it is the next
`character' to
be read in. So we will go through the loop one more time. This time the
call to
fgets will fail due to hitting the end-of-file as the first character (and
thus not
placing anything in the output buffer). The contents the buffer are left
unmodified
and will be processed by the remainder of the loop body. Now the next time
through
the loop, feof will return true and the loop will be exited - thus the
problem of
reading the last line twice. You can see this behaviour in the following
code
snippet:
while(!feof(fp))
{
//memset((void*)buffer, '\0', BUFFER_SIZE);
fgets(buffer, BUFFER_SIZE, fp);
buffer[strlen(buffer)-1] = '\0'; // eat new line
printf("***%s***",buffer);
}
Now if the input file contains the lines (where ^ represents a new line):
123456^
234567^
345678^
The output is:
***123456***
***234567***
***345678***
***34567***
(the last line is truncated due to the logic to eat the new line, the
contencts
of buffer is shortened by one twice).
If the memset line is uncomment, the output is:
***123456***
***234567***
***345678***
******
We have not gained much, at least the last line will not be reprocessed
since we
clear the buffer prior to each read. But it does not really solve your
problem
(yet). The hint, is to look at the return value of fgets which is either
the
buffer or NULL on error of if EOF is encountered. With this, we can change
the
logic of the above loop to:
while(!feof(fp))
{
memset((void*)buffer, '\0', BUFFER_SIZE);
if(NULL == fgets(buffer, BUFFER_SIZE, fp))
{
if(ferror(fp))
{
fprintf(stderr, "fgets failed.");
}
break;
}
else
{
int nLen = strlen(buffer) - 1;
if(buffer[nLen] == '\n') buffer[nLen] = '\0';
printf("***%s***",buffer);
}
}
Using the above code, the output is:
***123456***
***234567***
***345678***
Also, with the change in logic of handling new-lines, we can now handle
files of
the sort:
123456^
234567^
345678
i.e. - files that do not end with a new line.
Hope this helps,
George
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2004-09-15 14:57 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2004-09-15 9:39 feof fails Christian Stalp
2004-09-15 11:13 ` Pankaj
2004-09-15 14:23 ` Charlie Gordon
-- strict thread matches above, loose matches on Subject: below --
2004-09-15 14:57 Huber, George K RDECOM CERDEC STCD SRI
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).