problem with fread/fwrite

Phong Vo[drew] kpv at ulysses.att.com
Thu Nov 8 04:40:04 AEST 1990


In article <14384 at smoke.brl.mil>, gwyn at smoke.brl.mil (Doug Gwyn) writes:
- In article <402 at bally.Bally.COM> siva at bally.Bally.COM (Siva Chelliah) writes:
- -  fp = fopen("temp.dat","r+b");
- -  fread(tbuf,sizeof(tbuf),1,fp);
- -  fwrite(buf,sizeof(buf),1,fp);
- -line 0line 1line 2line 3line 4line 0line 4
- -Can you believe this?
- 
- Sure.
- 
- -When I used fseek before fwrite, it worked.  I do not remember reading 
- -anywhere that I should do a fseek before fread/fwrite.
- 
- The exact requirement is spelled out quite explicitly in the C standard,
- section 4.9.5.3.  I won't bore you with the technical reasons, but this
- was not an oversight nor necessarily sloppiness on the part of your C vendor.

However, one may argue that the sloppiness is in the C standard.
The standard, in this case, basically just documents the behavior of stdio
without considering that this is a bad design that arose from
a bad implementation. It is ugly to have to call fseek before switching modes.
There are other uglinesses (e.g., inconsistent interfaces) in stdio that
could have been avoided too. One may say that the standard failed in that
respect. This is sad considering that the standard did go a long way to
invent a new C language.

	Phong Vo



More information about the Comp.lang.c mailing list