title: How do I delete bytes from a file in C or C++?
body: <p>I've been Googling this for hours...reading and reading and reading, and yet nothing I come across seems to answer this simple question: <strong>In C or C++ programming: I have a file, it contains "hello world". I want to delete "world" (like pressing <kbd>Backspace</kbd> in a text editor), then save the file. How do I do this?</strong></p>
  
  <p>I know that files are <em>streams</em> (<a href="https://stackoverflow.com/questions/1216380/what-is-a-stream/1216400#1216400"><em>excellent</em> info on that here!</a>), which don't seem to have a way to delete items from a file per say, and I've studied all of the file-related functions in stdio.h: <a href="http://www.cplusplus.com/reference/cstdio/fopen/" rel="nofollow noreferrer">http://www.cplusplus.com/reference/cstdio/fopen/</a>. </p>
  
  <p>It seems to me that files and streams therefore are NOT like arrays: I can't just delete a byte from a file! Rather (I guess?) I have to create an entire new file and copy the whole original file into the new file withOUT the parts I want to delete? Is that the case?</p>
  
  <p>The only other option I can think of is to seek to the position before "world", then write binary zeros to the end of the file, thereby overwriting "world". The problem with this, however, is a text editor will now no longer properly display this file, as it has non-printable characters in it--and the file size hasn't shrunk--it still contains these bytes--it's just that they hold zeros now instead of ASCII text, so this doesn't seem to be right either.</p>
  
  <h1>Related</h1>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/17182147/resizing-a-file-in-c">Resizing a file in C++</a></li>
  </ul>