C++: Correct use of stream operations
<p>I am trying to learn to use the file stream correctly. The following is a snippet of code that I am maintaining. It works fine, but I was wondering whether the usage is correct and whether there are better ways to perform these tasks. For example, the use of SEEK_SET and ios::beg together. Also, is ios::ate really needed given that the next seek is to the beginning of file.</p>

<p>Could you please help me correct(streamline) the code.</p>

<pre><code>ofstream fout(fpath,ios::out| ios::binary | ios::ate);
fout.seekp(SEEK_SET,ios::beg);
fout.write((char*)&amp;test,sizeof(test));

fout.seekp(sizeof(off_t),ios::beg);
fout.write((char*)&amp;temp,sizeof(temp)); //temp is int

fout.seekp((sizeof(off_t)+ sizeof(int)),ios::beg);
fout.write((char*)o,sizeof(MyClass));  

fout.seekp((sizeof(off_t)+ sizeof(int) + sizeof(MyClass)),ios::beg);
fout.write((char*)&amp;l,sizeof(int)); 

fout.close();
</code></pre>
