C++ Writing to file vector of byte
c++
Solution
To store a vector in a file, you have to write the contents of the vector, not the vector itself. You can access the raw data with `&vector[0]`, address of the first element (given it contains at least one element).
ofstream outfile(filename, ios::out | ios::binary);
outfile.write(&data[0], data.size());
This should be fairly efficient at writing. `fstream` is generic, use `ofstream` if you are going to write.
Problem
I have: ``` typedef unsigned char; std::vector<byte> data; ``` I tried to save data in file this way (but I have error): ``` fstream file(filename,ios::out); file.write(&data, data.size()); ``` How to process or cast data to write it in file.