title: How to write file at specific position?
body: <p>I made a function in C to create a file using Unix command dd:<br>
      <code>dd if=/dev/zero of=file.data bs=8 count=32</code>  </p>
  
  <p>This works and created <code>file.data</code> with size <code>256 bytes</code> and if I open the file I can see it's empty.  </p>
  
  <p>Now I want to write to a specific position in this file using <code>fseek</code> and <code>fwrite</code>, but whenever I try to write to a position different from 0, it does nothing.  </p>
  
  <p>For example, If I want to write to position 2, I must also write to position 0 and 1.  </p>
  
  <pre><code>void createFile() {
      char command[100];
      sprintf(comando, "dd if=/dev/zero of=file.data bs=8 count=32");
      system(command);
  }
  
  void writeFile(int position, char * data) {
      FILE * file = fopen("file.data", "r+");
      fseek(file, position, SEEK_SET);
      fwrite(data, strlen(data), 1, file);
      fclose(file);
  }
  </code></pre>
  
  <p>Some examples  </p>
  
  <p>Input:  </p>
  
  <pre><code>writeFile(0, "0");
  writeFile(1, "1");  
  writeFile(2, "2");
  
  output &gt; 012
  </code></pre>
  
  <p>Input:  </p>
  
  <pre><code>writeFile(2, "2");
  writeFile(1, "1");
  writeFile(0, "0");
  
  output &gt; 012
  </code></pre>
  
  <p>Input:  </p>
  
  <pre><code>writeFile(1, "1");
  writeFile(2, "2");
  
  output &gt; empty
  </code></pre>
  
  <p>Is there some way to write into the file without having to write into the previous positions also?</p>