title: C++: Patching a binary file using fstream
body: <p>I have a huge file that is already created. I need to write some data at the start of the file while retaining the other contents of the file as such. The following code corrupts the existing file. Can anyone help me with the right method.</p>
  
  <pre><code> ofstream oFile(FileName,ios::out|ios::binary);  
   oFile.seekp(0);  
   oFile.write((char*)&amp;i,sizeof(i));       
   oFile.write((char*)&amp;j,sizeof(i));
   oFile.close();
  </code></pre>
  
  <p>EDIT:
  Basically I want to overwrite some bytes of an already existing file at different locations including start. I know the byte address of locations  to write. My write will not change the file size.</p>
  
  <p>I need to do something equivalent to the following code that works:</p>
  
  <pre><code>int  mode = O_RDWR;
  int myFilDes = open (FileName, mode, S_IRUSR | S_IWUSR);
  lseek (myFilDes, 0, SEEK_SET);
  write (myFilDes, &amp;i, sizeof (i));
  write (myFilDes, &amp;j, sizeof (j));
  </code></pre>