Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You are falling foul of a restriction upon the intermixing of read and write operations on a file opened in update mode that MS's <code>fstream</code> library inherits from from the its C <code>&lt;stdio.h&gt;</code> implementation.</p> <p>The C Standard (I cite C99, but it doesn't differ in this point from C89) at 7.19.5.3/6 states:</p> <blockquote> <p>When a file is opened with update mode ('+' as the second or third character in the above list of mode argument values), both input and output may be performed on the associated stream. However, output shall not be directly followed by input without an intervening call to the fflush function or to a file positioning function (fseek, fsetpos, or rewind), and <em>input shall not be directly followed by output without an intervening call to a file positioning function</em>, unless the input operation encounters end- of-file.</p> </blockquote> <p>(my emphasis).</p> <p>So your <code>stream.seekp(1)</code> solution, which devolves to a C <code>fseek</code>, is correct.</p> <p>The GNU C library does not have this Standard limitation, so your code as posted works as expected when built with GCC.</p> <p>The MS <code>&lt;fstream&gt;</code> library is compliant with the C++ Standard in inheriting this restriction. <code>fstream</code>s are implemented using <code>basic_filebuf&lt;charT,traits&gt;</code>. In the (C++11) Standard's account of this template, at § 27.9.1.1/2, it simply says:</p> <blockquote> <p>The restrictions on reading and writing a sequence controlled by an object of class basic_filebuf are the same as for reading and writing with the Standard C library FILEs.</p> </blockquote>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload