Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7846459
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T17:35:13+00:00 2026-06-02T17:35:13+00:00

My problem is that using ifstream read and fread on a file descriptor don’t

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T17:35:15+00:00Added an answer on June 2, 2026 at 5:35 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a program that load data from a file using std::ifstream and store
My root problem is that when using calls Dispose on a StreamWriter , it
My problem is that I am using the .hover method to get the x
I'm facing a problem that seems to have no straighforward solution. I'm using java.util.Map
I have a JQuery (using JQuery 1.4.2) problem that exhibits only in IE8 in
I'm using Entity Framework in my project, and I have the problem that, once
I am using swfupload to upload files on a website, the problem that every
I want to make a windowless application. The problem is that I'm using a
I am using TIdHttpServer to process some commands the problem is that some commands
I'm using masonry plugin in my website My problem is that the first 20

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.