basic_filebuf (and therefore fstream) does not allow you to put a write
operation directly after a read operation, or vice versa. Instead, the write
and the read must be separated by a seek or tell (tell being equivalent to a
seek-to-current-position).

As the Standard specifies behavior in terms of the file's contents, the current
position in the file, and get and put areas, this requirement has no basis in
it. basic_filebuf::underflow is required to return the next character in the
pending sequence, regardless of the contents of the put area. (27.5.2.4.3/8-9)

The problem lies in the implementation of the state machine of _M_reading and
_M_writing. These variables should be redundant: we are reading if the get area
is non-empty, and writing if the put area is non-empty. For example, underflow
is bracketed by

      const bool __testin = _M_mode & ios_base::in;
      if (__testin && !_M_writing)
        {

Reading after a write operation causes an underflow with an empty get area. If
the user has not explicitly flushed the buffer with seekoff or sync, the
current implementation refuses to switch modes. (And sync does not work for
writing after reading, as it does nothing unless the put area is not empty.)

It looks like the fix is to eliminate _M_reading and _M_writing in favor of
member functions that test the get and put areas, and calling overflow and
underflow to ensure prerequisites.


-- 
           Summary: fstream reads after writes, or vice versa, don't work
           Product: gcc
           Version: 4.5.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: potswa at mac dot com


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=45708

Reply via email to