Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!swrinde!zaphod.mps.ohio-state.edu!pacific.mps.ohio-state.edu!linac!att!emory!gatech!mcnc!rti!dg-rtp!dg-rtp.dg.com!hagins From: hagins@dg-rtp.dg.com (Jody Hagins) Newsgroups: comp.lang.c Subject: Re: problem with fread/fwrite Keywords: fread, fwrite, file pointers Message-ID: <1990Nov7.161507.25081@dg-rtp.dg.com> Date: 7 Nov 90 16:15:07 GMT References: <402@bally.Bally.COM> Sender: usenet@dg-rtp.dg.com (Usenet Administration) Reply-To: hagins@dg-rtp.dg.com (Jody Hagins) Organization: Data General Corporation, Research Triangle Park, NC Lines: 57 In article <402@bally.Bally.COM>, siva@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@gamecock.rtp.dg.com