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

  • SEARCH
  • Home
  • 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 8208329
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T09:18:09+00:00 2026-06-07T09:18:09+00:00

I am having a vector whose size can be really big (1 million elements).

  • 0

I am having a vector whose size can be really big (1 million elements). I am wrote the contents of the vector to a file as byte values. I am not able to figure out how I can read the byte values back into the vector.

Here is the code:

#include <fstream>
#include <vector>
#include <iterator>
#include <iostream>
using namespace std;

int main()
{
  // Filling a vector with values
  std::vector<bool> ve;
  ve.push_back(true);
  ve.push_back(false);
  ve.push_back(true);
  ve.push_back(false);
  ve.push_back(true);
  // Printing the values of the vector
  for(unsigned int i = 0; i < ve.size(); i++)
      cout << ve.at(i) << ".";
  cout << endl;

  // Writing the vector contents to a file
  const char* file_name = "abc.txt";
  ofstream outfile(file_name, ios::out | ios::binary);
  outfile.write((const char*)&(ve[0]), ve.size());
  outfile.close();
  // Reading the file and filling the vector with values
  ifstream infile ("abc.txt", ifstream::binary);
  vector<bool> out_ve((std::istreambuf_iterator<char>(infile)),
                       std::istreambuf_iterator<char>());

  while( !infile.eof() )
      out_ve.push_back(infile.get());

  // Checking if the values read are the same as the original values
  cout << "SIZE: " << out_ve.size() << endl;
  for(unsigned int i = 0; i < out_ve.size(); i++)
    cout << out_ve.at(i) << ".";
  cout << endl;

  infile.close();
  return 0;
}

[edit] Closed the file after writing and the output is very different from the input.

1.0.1.0.1.
SIZE: 6
1.1.1.0.1.1.

How can I get the correct elements into the vector out_ve?

  • 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-07T09:18:11+00:00Added an answer on June 7, 2026 at 9:18 am

    Writing data from most STL containers cannot be done with outfile.write((const char*)&(ve[0]), ve.size()); because they manage their memory in complex ways that’s fundamental to the way they operate. With vector, it works, because memory storage is contiguous, but vector<bool> is special because of the way it packs multiple bools into a single byte. As commenters have already pointed out, ve[0] returns a special temporary quasi-reference type, and writing that reference out by casting to a char* will produce something totally unrelated to the data that’s in the vector.

    Even if this construction gave you access to the raw memory of the vector, the code that you’re using to write out the data is incompatible with the code that you’re using to read in the data. The code that you’re using to write out the data would pack 8 bool entries into each char, but the code you’re using to read in the data converts each char into a single bool.

    Since you’re reading back your data using an istreambuf_iterator, why not write it out the same way:

    std::copy(ve.begin(), ve.end(), std::ostreambuf_iterator<char>(outfile));
    

    This writes out one bool per byte.

    If you want to write out data in a packed representation that writes one bit per bool, I think you’ll need to invent your own input and output iterators.

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

Sidebar

Related Questions

I'm having trouble getting the right size of a vector with struct elements. The
I'm having some trouble reading data from a file into a vector of Orders.
I'm having trouble understanding why this doesn't sort the vector by the time/size of
I wrote a code ( c++,visual studio 2010) which is having a vector, even
I'm having a std::vector with elements of some class ClassA . Additionally I want
I'm having a vector like this Vector = a:2.34;b:1.24;c:0.67 . How can I get
In R, when having a redundantly named vector, why is it not possible to
I am having some trouble with vector declarations in the header file of a
I'm trying to initialize a std::vector from a std::list efficiently, but I'm not having
I a having a little trouble with vector or array operations. I have three

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.