473,396 Members | 1,770 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 6280
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Tuuska | last post by:
Hello! I have this problem, when fwrite() writes to the beginning of xyz.txt file, it overwrites the first line. Any ideas how to prevent this? I'm running PHP 4.3.3RC3 on Linux. <?php $fp =...
8
by: Brady | last post by:
Hi, I'm having a problem reading and writing to a file. What I'm trying to do is read a file, modify the portion of the file that I just read, and then write the modified data back to the same...
23
by: FrancisC | last post by:
how to use fwrite( ) instead of fprintf( ) in this case? I want to generate binary file. FILE *fnew; int i, intName; double array; fprintf(fnew, "%d\n", intName);...
10
by: cs | last post by:
if i have typedef struct{ unsigned a; unsigned *b; }tp; i would like to ask if it is ok for the "C standard 89" doing something like this {
4
by: janssenssimon | last post by:
//de structure om de highscores in op de slagen typedef struct score{ char *naam; int veld; int score; struct score *volg; }HIGH; void toonhighscores(void)
2
by: elisa | last post by:
Dear all, I have problems in writeing and reading a block of data (long array) with fread and fwrite. If I write and read an integer array, everything looks fine, but when I try long array, sth...
30
by: empriser | last post by:
How to use fread/fwrite copy a file. When reach file's end, fread return 0, I don't konw how many bytes in buf.
12
by: Medvedev | last post by:
FILE *a,*b; int z; char str1; char str2; a= fopen(argv,"rb"); //Both files are EXE's b= fopen(argv,"rb+"); z= fread(str1,1,1,a); if (z !=1)
4
by: Highlander2nd | last post by:
Hello there. I'm Andrew Lucas, I'm a programmer for Half-Life. I've been working on stencil shadows lately, and I've been having problems saving mesh data for my models. When I store mesh data, I...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.