title: Compare two files Byte by Byte
body: <p>I have two binary files and I want to compare them Byte by Byte. I came up with the following code to do so:</p>
  
  <pre><code>int CompareFiles(char *pFname1, char *pFname2)
  {
      FILE      *pFile1,*pFile2;
      long      lSize1, lSize2;               // file length
      int       i=0;
      char      tmp1, tmp2;
  
      pFile1 = fopen(pFname1,"r");
      pFile2 = fopen(pFname2,"r");
  
      // obtain file size:
      fseek (pFile1 , 0 , SEEK_END);
      lSize1 = ftell (pFile1);
      rewind (pFile1);
  
      // obtain file size:
      fseek (pFile2 , 0 , SEEK_END);
      lSize2 = ftell (pFile2);
      rewind (pFile2);
  
      if (lSize1 != lSize2) {
          printf("File sizes differ, %d vs. %d\n",lSize1,lSize2);
          return ( ERROR );
      }
      for (i=0;i&lt;lSize1;i++) {
          fread(&amp;tmp1, sizeof(char), 1, pFile1+i);
          fread(&amp;tmp2, sizeof(char), 1, pFile2+i);
          if (tmp1 != tmp2) {
              printf("%x: tmp1 0x%x != tmp2 0x%x\n",i , tmp1, tmp2);
          }
      }
      return ( OK );
  }
  </code></pre>
  
  <p>But for some reason, it looks like the pointer in the file doesn't advance and it keeps comparing the same Bytes to each other for the whole length of the <code>for</code> loop. Why so? What am I doing wrong here?</p>