|
From: Ioannis V. <no...@ya...> - 2001-03-17 20:22:07
|
You can also use :
do
{
/* ... */
}while(!feof(filePtr));
In this way you gain one less check for end of file (where performance is
important).
About the second read of the "same data" at the end:
This happens because the end of file is true only after a failed attempt
(because the end of file has been already reached and it has no more to
read). That means it doesn't read the data for the second time, but you have
a failed attempt to read more data.
As for the counter you can either decrease it by one after the loop or
initialize it with -1.
Ioannis
* Ioannis Vranos
* Programming pages: http://members.nbci.com/noicys
* Alternative URL: http://run.to/noicys
-----Original Message-----
From: dev...@li...
[mailto:dev...@li...]On Behalf Of Sam
Sent: Saturday, March 17, 2001 8:01 PM
To: dev...@li...
Subject: [Dev-C++] feof question
I'm trying to read through a file using the following statement:
while(!feof(filePtr)
{
fread(&phone, sizeof(struct record), 1, filePtr);
printf("%d%s", int, name);
count++;
}
The problem is that it reads the last record twice and increments the
count one more time than needed. Could someone explain why this happens and
how to avoid it using the feof function? Thanks.
Sam
|