title: fwrite function writing data wrongly
body: <p>I'm writing a program to read a specified piece of data from one file and write it into a specified offset of a second file, but I have a problem. The <code>fwrite</code> function for some reason isn't writing the data read from the first file. Instead, it writes a non-printing character (possibly null character, idk) at the offset of the second file. Here's the code:</p>
  <pre><code>int main(int argc, char **args)
  {
      if (argc &lt; 7)
      {
          print_usage(args[0]);
          exit(EXIT_FAILURE);
      }
      else if (argc &gt; 7)
      {
          printf(&quot;Too many parameters\n&quot;);
          
          exit(EXIT_FAILURE);
      }
      
      if (geteuid() != 0)
      {
          printf(&quot;This program requires root privileges\n&quot;);
          
          exit(EXIT_FAILURE);
      }
      
      unsigned long offset;
      unsigned long size;
      char *partpath;
      char *filepath;
      
      int c;
      
      for (c = 0; c &lt; argc; c++)
      {
          char *arg = args[c];
          
          if (strcmp(arg, &quot;-o&quot;) == 0)
          {
              offset = strtoul(args[++c], NULL, 0);
              
              continue;
          }
          
          if (strcmp(arg, &quot;-s&quot;) == 0)
          {
              size = strtoul(args[++c], NULL, 0);
              
              continue;
          }
          
          if (c == argc - 2)
              partpath = args[c];
          if (c == argc - 1)
              filepath = args[c];
      }
  
      printf(&quot;offset: %lu (0x%lx)\nsize: %lu (0x%lx)\npartition: %s\nfile: %s\n&quot;, offset, offset, size, size, partpath, filepath);
      
      flash_part(partpath, offset, filepath, size);
  
      return 0;
  }
  
  void flash_part(const char *partition_path, unsigned long offset, const char *file_to_flash, unsigned long size)
  {
      log(&quot;Opening files...&quot;);
      FILE *partition = fopen(partition_path, &quot;r+&quot;);
      
      FILE *flash_file = fopen(file_to_flash, &quot;r&quot;);
      
      log(&quot;Jumping to partition offset...&quot;);
      // jump to partition offset
      fseek(partition, offset, SEEK_SET);
      
      // start flashing chunk by chunk
  
      log(&quot;Start flashing...&quot;);
      void *chk = calloc(1, sizeof(void*));
      size_t count = read_chk(flash_file, chk);
      
      printf(&quot;readed %d bytes\n&quot;, count);
      
      write_chk(partition, chk,  count);
      free(chk);
      
      while (count &gt;= CHK_SIZE)
      {
          chk = calloc(1, sizeof(void*));
          count = read_chk(flash_file, chk);
          
          write_chk(partition, chk, count);
          
          free(chk);
      }
      
      log(&quot;Partition flashed, closing files...&quot;);
      
      fclose(partition);
      fclose(flash_file);
  }
  
  size_t read_chk(FILE *file, void *out)
  {
      char *chk_data = calloc(CHK_SIZE, 1);
      
      size_t readed = fread(chk_data, 1, CHK_SIZE, file);
      
      out = chk_data;
  
      return readed;
  }
  
  void write_chk(FILE *file, void *data, unsigned int size)
  {
      fwrite(data, 1, size, file);
  }
  </code></pre>
  <p>What's wrong with it?</p>