problem with fread/fwrite

Jody Hagins hagins at dg-rtp.dg.com
Thu Nov 8 03:15:07 AEST 1990


In article <402 at bally.Bally.COM>, siva at bally.Bally.COM (Siva Chelliah) writes:
|> 
|> I have an interesting (?) question.  

[ program 1 deleted ]

|> fread should update the  pointer, so that I should be able to do a read or 
|> a write after that. Right ?

Yes.

|> Program 2 :
|> 
|> #include "stdio.h"
|> char buf[7] = "line 4";
|> char tbuf[7];
|> main ()
|> {
|>   int i=1;
|>   FILE *fp;
|>   fp = fopen("temp.dat","r+b");
|>   fread(tbuf,sizeof(tbuf),1,fp);
|>   printf("tbuf = %s\n",tbuf);     /* this worked . I got line 0 */
|>   fwrite(buf,sizeof(buf),1,fp);
|>   fclose(fp);
|> }
|> 
|> temp.dat : |> 
|> line 0line 1line 2line 3line 4line 0line 4
|> 
|> Can you believe this?  This happened when I used IBM RT, AIX 2.0
|> When I used Microsoft C 5.1(DOS 3.3 ) , nothing changed in temp.dat .
|> When I used fseek before fwrite , it worked.  I do not remember reading 
|> anywhere that I should do a fseek before fread/ fwrite.  Is that a
bug in the 
|> compiler or in my head ?  Please help.

Would you believe, in your head?
The following is a quote from "C A Reference Manual" by 
Harbison and Steele pertaining to fopen().

"When a file is opened for update ('+' is present in the type
string), the resulting stream may be used for both input and
output.  However, an output operation may not be followed by
an input operation without an intervening call to fseek() or
rewind(), and an input operation may not be followed by an
output operation without an intervening call to fseek() or
rewind() or an input operation that encounters end-of-file"

Hope this helps!

|> 
|> Siva


	-Jody
	hagins at gamecock.rtp.dg.com



More information about the Comp.lang.c mailing list