problem with fread/fwrite

Rahul Dhesi dhesi%cirrusl at oliveb.ATC.olivetti.com
Mon Nov 12 15:52:11 AEST 1990


In <13992 at ulysses.att.com> kpv at ulysses.att.com (Phong Vo[drew]) writes:

   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.

I believe the requirement to call fseek (etc.) when switching arises
out of the need to make stdio fast.  Due to buffering, alternating
reads and writes can confuse each other.  The only way the stdio
library could automatically protect you against this would be for it to
explicitly test for internal state before every read and write.  E.g.,
within fread, we sould have:

     if (my_state == DOING_WRITE) {
        .. resync buffer ..
        my_state = DOING_READ;
        .. rest of fread ..
     }

I suppose we should consider ourselves lucky we are even allowed to do
both reads and writes on the same data stream:

     I had the blues
     because I had no shoes
     Until upon the street
     I met a man whose feet
     were stuck in Pascal.
--
Rahul Dhesi <dhesi%cirrusl at oliveb.ATC.olivetti.com>
UUCP:  oliveb!cirrusl!dhesi



More information about the Comp.lang.c mailing list