title: ifstream read and fread not returning same data, C++
body: <p>My problem is that using ifstream read and fread on a file descriptor don't seem to produce the same results.</p>
  
  <p>I open a file and read its input using ifstream open/read in ios::binary mode.  Then I write this buffer out to a file. out1.</p>
  
  <p>Next, I open the same file, read its input using FILE* file descriptors and fread.  Then I write this buffer out to another file, out2.</p>
  
  <p>When I compare out1 to out2 they do not match.  out2, which uses FILE*, seems to stop reading, near the end.  </p>
  
  <p>More worrisome is that neither buffer matches the input file.  The ifstream::read method seems to be modifying the end of line characters, even though I open the input file as ios::binary.</p>
  
  <p>The fread method seems to be returning a value less than length (199) even though it's reading significantly more characters than that, as I can see the buffer that got read.  This doesn't match the length determined by the seekg commands.</p>
  
  <p>I'm quite confused and any help would be appreciated.  Code is attached.</p>
  
  <p>Thanks!
  -Julian</p>
  
  <pre><code>   ifstream read_file;
     read_file.open("V:\\temp\\compressiontest\\out\\test_20224-5120_256x256.jpg", ios::binary);
  
     read_file.seekg(0, ios::end);
     unsigned long length = read_file.tellg();
     cout &lt;&lt; "Length: " &lt;&lt; length &lt;&lt; endl;
     read_file.seekg(0, ios::beg);
  
     unsigned char* buffer = new unsigned char[length];
     unsigned char* buf = new unsigned char[length];
     for(int i = 0; i &lt; length; i++)
     {
        buffer[i] = 0;
        buf[i] = 0;
     }
  
     if(read_file.is_open())
     {
        read_file.read((char*)buffer, length);
     }
     else
     {
        cout &lt;&lt; "not open" &lt;&lt; endl;
     }
  
     read_file.close();
  
     FILE* read_file_1 = NULL;
     read_file_1 = fopen("V:\\temp\\compressiontest\\out\\test_20224-5120_256x256.jpg", "r");
     size_t read_len = fread(buf, 1, length, read_file_1);
     fclose(read_file_1);
  
     if(read_len != length)
        cout &lt;&lt; "read len != length" &lt;&lt; " read_len: " &lt;&lt; read_len &lt;&lt; " length: " &lt;&lt; length &lt;&lt; endl;
  
     int consistent = 0;
     int inconsistent = 0;
     for(int i = 0; i &lt; length; i++)
     {
        if(buf[i] != buffer[i])
           inconsistent++;
        else
           consistent++;
     }
  
     cout &lt;&lt; "inconsistent:" &lt;&lt; inconsistent &lt;&lt; endl;
     cout &lt;&lt; "consistent:" &lt;&lt; consistent &lt;&lt; endl;
  
     FILE* file1;
     file1 = fopen("V:\\temp\\compressiontest\\out1.jpg", "w"); 
     fwrite((void*) buffer, 1, length, file1);
     fclose(file1);
  
     FILE* file2;
     file2 = fopen("V:\\temp\\compressiontest\\out2.jpg", "w"); 
     fwrite((void*) buf, 1, length, file2);
     fclose(file2);
  
     return 0;
  </code></pre>