28

That is the point. How to write and read binary files with std::vector inside them?

I was thinking something like:

//============ WRITING A VECTOR INTO A FILE ================
const int DIM = 6;
int array[DIM] = {1,2,3,4,5,6};
std::vector<int> myVector(array, array + DIM);
ofstream FILE(Path, ios::out | ofstream::binary);
FILE.write(reinterpret_cast<const char *>(&myVector), sizeof(vector) * 6);
//===========================================================

But I don't know how to read this vector. Because I thought that the following was correctly but it isn't:

ifstream FILE(Path, ios::in | ifstream::binary);
FILE.read(reinterpret_cast<const char *>(&myVector), sizeof(vector) * 6);

So, how to perform the operation?

5 Answers 5

37

Try using an ostream_iterator/ostreambuf_iterator, istream_iterator/istreambuf_iterator, and the STL copy methods:

#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>

#include <fstream> // looks like we need this too (edit by π)

std::string path("/some/path/here");

const int DIM = 6;
int array[DIM] = {1,2,3,4,5,6};
std::vector<int> myVector(array, array + DIM);
std::vector<int> newVector;

std::ofstream FILE(path, std::ios::out | std::ofstream::binary);
std::copy(myVector.begin(), myVector.end(), std::ostreambuf_iterator<char>(FILE));

std::ifstream INFILE(path, std::ios::in | std::ifstream::binary);
std::istreambuf_iterator iter(INFILE);
std::copy(iter.begin(), iter.end(), std::back_inserter(newVector));
5
  • 5
    What is the type parameter on that vector? Is it a vector of chars? Or of ints? If it isn't a vector of chars, how does copy write it to an output iterator of chars? Does it just downconvert every item to a single byte?
    – interfect
    Commented Aug 6, 2014 at 21:43
  • 1
    std::istreambuf_iterator iter(INFILE); doesn't work on MSVC2016?
    – user1024
    Commented Mar 5, 2016 at 8:02
  • 1
    @user1024 I've posted a full version of this program here
    – wally
    Commented Apr 8, 2016 at 18:36
  • I concur with user1024--VS 2013 compiler doesn't like taking the ifstream as an argument to the iterator instance. Commented Aug 16, 2017 at 16:30
  • This is absolutely dangerous. What if array[0] = 256 ? An int won't fit in a byte ofc, and 0x00 will be written instead Commented Sep 13, 2018 at 7:43
5

Use boost::serialization.

If you don't want use boost - write size and vector.

size_t sz = myVector.size();
FILE.write(reinterpret_cast<const char*>(&sz), sizeof(sz));
FILE.write(reinterpret_cast<const char*>(&myVector[0]), sz * sizeof(myVector[0]));
0
4

You can use

#include <boost/serialization/vector.hpp>

to serialize your vector. Read a tutorial here: http://www.boost.org/libs/serialization/doc/tutorial.html#stl `

1
  • 2
    It's worth mentioning that Boost.Serialization might significantly affect both build times and the binary size. So if the TS need only to save a vector to a file, using serialization would be overkill.
    – Igor R.
    Commented Sep 11, 2012 at 15:08
2

Before reading vector, you should resize it: yourVector.size(numberOfElementsYouRead).

Besides, sizeof(vector<your_type>) is just the size of the vector object internal implementation; vector element size is sizeof(std::vector<your_type>::value_type).

Then read it like this:

file.read(reinterpret_cast<char *>(&myVector[0]), sizeof(vector<int>::element_type) * element_count); 
2
  • OK, so, you think the way I choosed( FILE.read(reinterpret_cast<const char *>(&myVector), sizeof(vector) * 6); ) is correct? Commented Sep 11, 2012 at 15:09
  • 2
    @facunvd 1) don't cast to const; 2) calculate the size correctly, according to the above. Of course, the approach proposed by @Platinum Azure is much better.
    – Igor R.
    Commented Sep 11, 2012 at 15:11
1

I used the fact that the data() method returns an address you can use for reading AND for writing.

// Assume outf is a writable filepointer (binary). // write myVector.size() to file, then

fwrite(myVector.data(), sizeof(decltype(myVector)::value_type), myVector.size(), outf);

to read:

// read MyVector.size() from file as nv, inpf is read filepointer

MyVector.resize(nv);
fread(MyVector.data(), sizeof(decltype(MyVector)::value_type), nv, inpf);

clinging to old ways in file io, but please ignore that (even if it might irritate you :)).

A weakness is that endianness is unsupported in this way.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.