EOF Problem

S

sugaray

hi, the problem i'm having here is that after i've written
10 integers to a disk file, the reading process always output
one more integer than was in the file.

===========snippet for write ===========
int i;
FILE *fp;
fp=fopen("number","a+b");

for(i=0;i<10;++i)
fwrite(&i,sizeof(int),1,fp);

fclose(fp);

========== snippet for read ===========
int i;
FILE *fp;
fp=fopen("number","rb");

while(!feof(fp)){
fread(&i,sizeof(int),1,fp);
printf("%d\n",i);
};

fclose(fp);
=======================================

thanx for your help !
 
L

Lew Pitcher

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
hi, the problem i'm having here is that after i've written
10 integers to a disk file, the reading process always output
one more integer than was in the file. [snip]
========== snippet for read ===========
int i;
FILE *fp;
fp=fopen("number","rb");

while(!feof(fp)){
fread(&i,sizeof(int),1,fp);
printf("%d\n",i);
};

fclose(fp);
=======================================

feof() only returns true /after/ you've read past end-of-file

Consider the following:
Assume that your file contains one (and only one) integer. Your program does
the following:
a) fopen the file
b) test if we have encountered eof on the file (feof())
c) because feof() is not true (we haven't read past eof yet)
fread() the first and only integer.
d) print integer
e) test if we have encountered eof on the file
f) since we haven't yet read past end-of-file (we're right /before/
end-of-file) feof() didn't return true, so we
fread() the second (non-existant) integer (this fails, silently, but also
now positions us such that we have /now/ read past eof)
g) print integer
h) test if we have encountered eof on the file
i) since we /have/ read past eof, we exit the loop
j) fclose
k) terminate





- --
Lew Pitcher

Master Codewright & JOAT-in-training | GPG public key available on request
Registered Linux User #112576 (http://counter.li.org/)
Slackware - Because I know what I'm doing.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQFA3j8HagVFX4UWr64RAl14AKDABZPxegr4GF4QRng2o7mXduVuqgCdE+YO
LfMASCNalHwkTQRiAqBDQSE=
=h2ah
-----END PGP SIGNATURE-----
 
A

Artie Gold

sugaray said:
hi, the problem i'm having here is that after i've written
10 integers to a disk file, the reading process always output
one more integer than was in the file.

===========snippet for write ===========
int i;
FILE *fp;
fp=fopen("number","a+b");

for(i=0;i<10;++i)
fwrite(&i,sizeof(int),1,fp);

fclose(fp);

========== snippet for read ===========
int i;
FILE *fp;
fp=fopen("number","rb");

while(!feof(fp)){
fread(&i,sizeof(int),1,fp);
printf("%d\n",i);
};

fclose(fp);
=======================================

See: http://www.eskimo.com/~scs/C-faq/q12.2.html

(you *did* read the FAQ before posting, didn't you? ;-))

HTH,
--ag
 
C

CBFalconer

sugaray said:
hi, the problem i'm having here is that after i've written
10 integers to a disk file, the reading process always output
one more integer than was in the file.

===========snippet for write ===========
int i;
FILE *fp;
fp=fopen("number","a+b");

for(i=0;i<10;++i)
fwrite(&i,sizeof(int),1,fp);

fclose(fp);

========== snippet for read ===========
int i;
FILE *fp;
fp=fopen("number","rb");

while(!feof(fp)){
fread(&i,sizeof(int),1,fp);
printf("%d\n",i);
};

fclose(fp);
=======================================

The problem is failure to read the faq, in particular 12.2. Try:

while (fread(&i, sizeof (int), 1, fp)) printf("%d\n", i);

--
Some useful references:
<http://www.ungerhu.com/jxh/clc.welcome.txt>
<http://www.eskimo.com/~scs/C-faq/top.html>
<http://benpfaff.org/writings/clc/off-topic.html>
<http://anubis.dkuug.dk/jtc1/sc22/wg14/www/docs/n869/> (C99)
 
E

Emmanuel Delahaye

In said:
hi, the problem i'm having here is that after i've written
10 integers to a disk file, the reading process always output
one more integer than was in the file.

It's a FAQ.
while(!feof(fp)){

Can you name the book or the school/course that taught that to you ? They
desserve to burn in hell.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

URGENT 1
error 28
Help with EXT3 Filesystem work 1
eof problems in read double data from a file 3
file bug 28
code snippet 92
fseek 17
comparison error 12

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,527
Members
44,998
Latest member
MarissaEub

Latest Threads

Top