468,736 Members | 1,780 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,736 developers. It's quick & easy.

fwrite() fails when called after fread()

// 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.

Apr 11 '06 #1
2 5840
In article <11**********************@u72g2000cwu.googlegroups .com>,
Richard Hsu <ri*********@gmail.com> wrote:
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.
--
I was very young in those days, but I was also rather dim.
-- Christopher Priest
Apr 11 '06 #2
thank you very much.

Walter Roberson wrote:
In article <11**********************@u72g2000cwu.googlegroups .com>,
Richard Hsu <ri*********@gmail.com> wrote:
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.
--
I was very young in those days, but I was also rather dim.
-- Christopher Priest


Apr 11 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Tuuska | last post: by
8 posts views Thread by Brady | last post: by
23 posts views Thread by FrancisC | last post: by
10 posts views Thread by cs | last post: by
4 posts views Thread by janssenssimon | last post: by
30 posts views Thread by empriser | last post: by
12 posts views Thread by Medvedev | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
xarzu
2 posts views Thread by xarzu | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.