Question: 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?

Answer: 
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); 

```
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.