fwrite() fails when called after fread()

R

Richard Hsu

// code
#include "stdio.h"
int status(FILE * f) {
printf("ftell:%d, feof:%s\n", ftell(f), feof(f) != 0 ? "true" :
"false");
}

int case1() {
FILE * f = fopen("c:\\blah", "wb+");
int i = 5;

printf("fwrite:%d\n", fwrite(&i,sizeof(int),1,f));

rewind(f);

fread(&i,sizeof(int),1,f);
status(f);
printf("fwrite:%d\n", fwrite(&i,sizeof(int),1,f)); // fails

fread(&i,sizeof(int),1,f); // push it to eof
status(f);
printf("fwrite:%d\n", fwrite(&i,sizeof(int),1,f)); // now successful

fclose(f);
}

int case2() {
FILE * f = fopen("c:\\blah", "wb+");
int i = 5;

printf("fwrite:%d\n", fwrite(&i,sizeof(int),1,f));

rewind(f);

fseek(f,sizeof(int),0); // similar to fread but don't read just move
file pointer
status(f);
printf("fwrite:%d\n", fwrite(&i,sizeof(int),1,f)); // this works

fclose(f);
}

int main() {
printf("running case1..\n");
case1();
printf("running case2..\n");
case2();

system("PAUSE");
}

//----------------------------------------------------------------------------------------
Hi All,

in case1,
when i do fread() and then fwrite, fwrite fails to write, it works only
when i do another fread() to push the file pointer to eof. can someone
help me understand this ?

in case2,
instead of using fread() if i use fseek() to advance the file pointer
ahead, fwrite() works without any problem.

i am using ftell() to tell me where the file pointer is at. in both
cases, the file pointer is at the same position however in case1 fwrite
fails while it works in case2,

so my question is
a) why fwrite fails ?
b) why it works when i use another redundant fread() to push it to eof
?
c) why it works in case2 with fseek even though ftell() shows the same
position ?

thank you in advance.

Richard Hsu
Toronto, Canada.
 
W

Walter Roberson

Richard Hsu said:
int case1() {
fread(&i,sizeof(int),1,f);
status(f);
printf("fwrite:%d\n", fwrite(&i,sizeof(int),1,f)); // fails

from the description of fopen()'s update mode ('+')

However, output may not be directly followed by input without
an intervening call to teh fflush function or to a file positioning
function (fseek, fsetpos, or rewind), and input may not be
directly followed by output without an intervening call to a
file positioning function, unless the input operation encounters
end-of-file.
 
R

Richard Hsu

thank you very much.

Walter said:
from the description of fopen()'s update mode ('+')

However, output may not be directly followed by input without
an intervening call to teh fflush function or to a file positioning
function (fseek, fsetpos, or rewind), and input may not be
directly followed by output without an intervening call to a
file positioning function, unless the input operation encounters
end-of-file.
 

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

fread/fwrite 2 18
fread fwrite struct 4
URGENT 1
Understanding fwrite() 10
error 28
Command Line Arguments 0
regarding lseek and fread 2
fread/fwrite Portability Issues 20

Members online

No members online now.

Forum statistics

Threads
473,999
Messages
2,570,243
Members
46,836
Latest member
login dogas

Latest Threads

Top