ifstream read and fread not returning same data, C++

拟墨画扇 提交于 2021-02-17 05:24:26

问题


My problem is that using ifstream read and fread on a file descriptor don't seem to produce the same results.

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.

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.

When I compare out1 to out2 they do not match. out2, which uses FILE*, seems to stop reading, near the end.

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.

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.

I'm quite confused and any help would be appreciated. Code is attached.

Thanks! -Julian

   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 << "Length: " << length << 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 < length; i++)
   {
      buffer[i] = 0;
      buf[i] = 0;
   }

   if(read_file.is_open())
   {
      read_file.read((char*)buffer, length);
   }
   else
   {
      cout << "not open" << 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 << "read len != length" << " read_len: " << read_len << " length: " << length << endl;

   int consistent = 0;
   int inconsistent = 0;
   for(int i = 0; i < length; i++)
   {
      if(buf[i] != buffer[i])
         inconsistent++;
      else
         consistent++;
   }

   cout << "inconsistent:" << inconsistent << endl;
   cout << "consistent:" << consistent << 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;

回答1:


You're calling fopen() for read using mode r instead of mode rb and for write using mode w instead of mode wb, which on Windows (default) means that you're both reading and writing with text translation, not in binary mode.



来源:https://stackoverflow.com/questions/10323321/ifstream-read-and-fread-not-returning-same-data-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!