title: Fastest way to delete the first few bytes of a file
body: <p>I am using a windows mobile compact edition 6.5 phone and am writing out binary data to a file from bluetooth.  These files get quite large, 16M+ and what I need to do is to once the file is written then I need to search the file for a start character and then delete everything before, thus eliminating garbage.  I cannot do this inline when the data comes in due to graphing issues and speed as I get alot of data coming in and there is already too many if conditions on the incoming data.  I figured it was best to post process.  Anyway here is my dilemma, speed of search for the start bytes and the rewrite of the file takes sometimes 5mins or more...I basically move the file over to a temp file parse through it and rewrite a whole new file.  I have to do this byte by byte.</p>
  
  <pre><code>private void closeFiles() {
      try {
  
      // Close file stream for raw data.
      if (this.fsRaw != null) {
          this.fsRaw.Flush();
          this.fsRaw.Close();
  
          // Move file, seek the first sync bytes, 
          // write to fsRaw stream with sync byte and rest of data after it
          File.Move(this.s_fileNameRaw, this.s_fileNameRaw + ".old");
          FileStream fsRaw_Copy = File.Open(this.s_fileNameRaw + ".old", FileMode.Open);
          this.fsRaw = File.Create(this.s_fileNameRaw);
  
          int x = 0;
          bool syncFound = false;
  
          // search for sync byte algorithm
          while (x != -1) {
              ... logic to search for sync byte
              if (x != -1 &amp;&amp; syncFound) {
                  this.fsPatientRaw.WriteByte((byte)x);
              }
          }
  
          this.fsRaw.Close();
  
          fsRaw_Copy.Close();
          File.Delete(this.s_fileNameRaw + ".old");
      }
  
  
      } catch(IOException e) {
          CLogger.WriteLog(ELogLevel.ERROR,"Exception in writing: " + e.Message);
      }
  }
  </code></pre>
  
  <p>There has got to be a faster way than this!  </p>
  
  <p>------------Testing times using answer -------------</p>
  
  <p>Initial Test my way with one byte read and and one byte write:</p>
  
  <pre><code>27 Kb/sec
  </code></pre>
  
  <p>using a answer below and a 32768 byte buffer:</p>
  
  <pre><code>321 Kb/sec
  </code></pre>
  
  <p>using a answer below and a 65536 byte buffer:</p>
  
  <pre><code>501 Kb/sec
  </code></pre>