Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you are trying to use the possibilities of the <em>stream</em>, for training purposes, then the code is fine.</p> <p>If you are just trying to write some data, then the <em>seekp()</em> method calls are not needed, since the writing pointer will be updated automatically to the end of the last write. <em>seekp()</em> is only needed if you need to navigate among records, as in a database, for example.</p> <pre><code>ofstream fout(fpath,ios::out| ios::binary | ios::ate); fout.write((char*)&amp;test,sizeof(test)); fout.write((char*)&amp;temp,sizeof(temp)); fout.write((char*)o,sizeof(MyClass)); fout.write((char*)&amp;l,sizeof(int)); fout.close(); </code></pre> <p>This code will work just fine. About problems in your code, you seem to be mixin the C library stdio, with its <em>fseek()</em> function, with the <em>seekp()</em> and <em>seekg()</em> methods. So, it is not very valid to pass SEEK_SET as the first parameter of your call to <em>seekp()</em>. In C, you would write:</p> <pre><code>#include &lt;cstdio&gt; //... fseek( f, 5, SEEK_SET ); // from the beginning fseek( f, -2, SEEK_CUR ); // from the current position fseek( f, 2, SEEK_CUR ); // from the current position fseek( f, -2, SEEK_END ); // from the end of the file </code></pre> <p>SEEK_SET will have an int value (implementation-specific, say 2), and therefore, with your line:</p> <pre><code>fout.seekp(SEEK_SET,ios::beg); </code></pre> <p>You're just starting say 2 bytes off the beginning, while your file may or may not have contents when you open it.</p> <p>About ios::ate, it puts the pointers for reading and writing at the end of the file (you would have to use seekp and seekg to achieve the same behaviour if you needed it, instead of using ios::ate).</p>
    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. 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.
 

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