title: I am trying to use fread and fwrite to make a copy function but I keep getting strange output depending on the buffer size
body: <p>I am trying to write a copy function which takes in three arguments when run in the command line. The program is run using the name of the program followed by the user specified buffer size and then the name of the input file and the name of the output file. </p>
  
  <p>E.g. >>copyf 1024 input.txt output.txt which are stored in argv[0] to argv[3] respectively.</p>
  
  <p>When I run the code with a buffer size of 1024 I get something like this:</p>
  
  <blockquote>
    <p>This is the beginning of the test file and I want to see how far it makes it in to the file before cutting stuff off.ÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌ</p>
  </blockquote>
  
  <p>When I run the code with a buffer size of 32 I get something like this:</p>
  
  <blockquote>
    <p>This is the beginning of the test file and I want to see how far it makes it in to the file before cutting stuff off.tting stuff</p>
  </blockquote>
  
  <p>Here is the code:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;time.h&gt;
  #include &lt;string&gt;
  
  int main(int argc, char*argv[])
  {
      int end = 0;
      char buffer[65536];
      //Error checking
      if (argc != 4)
      {
          end = 1;
          printf("Error-Insufficient Arguments (%d/4)\n", argc);
  
  
      }
      FILE*fp, *outFile;
      fp = fopen(argv[2], "r");
      outFile = fopen(argv[3], "a");
      if (fp == NULL)
      {
          end = 1;
          printf("Error-Input file is fake\n");
      }
  
      //No errors. Proceed with copy code
      int size = atoi(argv[1]);
      printf("Buffer size is: %d Bytes\n\n",size);
  
      if (end == 0)
      {
          printf("No Errors Detected\n\n");
          while (!feof(fp))
          {
  
              fread(&amp;buffer, size, 1, fp);
  
              fwrite(&amp;buffer, size, 1, outFile);
  
              for (int i = 0; i &lt; 65536; i++)
              {
                  buffer[i] = NULL;
              }
          }
  
      }
      fclose(fp);
      fclose(outFile);
  
      clock_t endTime = clock();
      float endTimeSec = endTime;
      printf("Runtime = %f\n\n", endTimeSec / 1000);
      return 0;
  }
  </code></pre>
  
  <p>Any suggestions on what I am doing wrong would be much appreciated. I have a feeling the source of the problem is with my understanding of how fread and fwrite work in this context. </p>