title: how to write an integer to a file (the difference between fprintf and fwrite)
body: <p>I've been trying to write an integer to a file (open mode is w).
  fprintf wrote it correctly but fwrite wrote gibberish:</p>
  
  <pre><code>int length;
  char * word = "word";
  
  counter = strlen(word);
  fwrite(&amp;length, sizeof(int), 1, file);
  fwrite(word, sizeof(char), length, file);
  </code></pre>
  
  <p>and the result in the file is:</p>
  
  <blockquote>
    <p>word</p>
  </blockquote>
  
  <p>but if I use fprintf instead, like this:</p>
  
  <pre><code>int length;
  char * word = "word";
  
  counter = strlen(firstWord);
  fprintf(file, "%d", counter);
  fwrite(word, sizeof(char), length, file);
  </code></pre>
  
  <p>I get this result in the file:</p>
  
  <blockquote>
    <p>4word</p>
  </blockquote>
  
  <p>can anyone tell what I did wrong?
  thanks!</p>
  
  <p>update: I would eventually like to change the writing to binary (I will open the file in wb mode), will there be a difference in my implementation?</p>