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?

David Rodríguez - dribeas
  • 204,818
  • 23
  • 294
  • 489
FacundoGFlores
  • 7,858
  • 12
  • 64
  • 94

5 Answers5

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));
P i
  • 29,020
  • 36
  • 159
  • 267
Platinum Azure
  • 45,269
  • 12
  • 110
  • 134
  • 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 Aug 06 '14 at 21:43
  • 1
    std::istreambuf_iterator iter(INFILE); doesn't work on MSVC2016? – user1024 Mar 05 '16 at 08:02
  • 1
    @user1024 I've posted a full version of this program [here](http://stackoverflow.com/a/36506833/1460794) – wally Apr 08 '16 at 18:36
  • I concur with user1024--VS 2013 compiler doesn't like taking the ifstream as an argument to the iterator instance. – user1329482 Aug 16 '17 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 – Patrizio Bertoni Sep 13 '18 at 07: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]));
ForEveR
  • 55,233
  • 2
  • 119
  • 133
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 `

Praetorian
  • 106,671
  • 19
  • 240
  • 328
piokuc
  • 25,594
  • 11
  • 72
  • 102
  • 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. Sep 11 '12 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); 
Igor R.
  • 14,716
  • 2
  • 49
  • 83
  • OK, so, you think the way I choosed( FILE.read(reinterpret_cast(&myVector), sizeof(vector) * 6); ) is correct? – FacundoGFlores Sep 11 '12 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. Sep 11 '12 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.

Jan
  • 447
  • 6
  • 8