title: C Delete last n characters from file
body: <p>I need to delete the last n characters from a file using C code. At fist I was trying to use '\b', but it returns a Segmentation Fault. I have seen interesting answers to similar questions <a href="https://stackoverflow.com/questions/584639/truncate-file">here</a> and <a href="https://stackoverflow.com/questions/1311683/is-there-any-basic-way-to-delete-something-from-opened-file">here</a>, but I would prefer to use <code>mmap</code> function to do this, if it's possible. I know it could be simpler to truncate the file by creating a temp file, and writing chars to temp until some offset of the original file. The problem is I don't seem to understand how to use <code>mmap</code> function to do this, can't see what parameters I need to pass to that function, specially <code>address</code>, <code>length</code> and <code>offset</code>. From what I've read, I should use <code>MAP_SHARED</code> in <code>flags</code> and <code>PROT_READ|PROT_WRITE</code> in <code>protect</code>. </p>
  
  <p>The function definition says: </p>
  
  <pre><code>void * mmap (void *address, size_t length, int protect, int flags, int filedes, off_t offset)
  </code></pre>
  
  <p>Here is my main:</p>
  
  <pre><code>int main(int argc, char * argv[])
  {
      FILE * InputFile;
      off_t position;
          int charsToDelete;
  
      if ((InputFile = fopen(argv[1],"r+")) == NULL)
      {
              printf("tdes: file not found: %s\n",argv[1]);
      }
      else
      {
                  charsToDelete = 5;
          fseeko(InputFile,-charsToDelete,SEEK_END);
          position = ftello(InputFile);
          printf("Pos: %d\n",(int)position);
          int i;
          //for(i = 0;i &lt; charsToDelete;i++)
          //{
          //  putc(InputFile,'\b');
          //}
      }
      fclose(InputFile);
      return 0;
  }
  </code></pre>