Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading and writing a std::vector into a file correctly

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?

like image 985
FacundoGFlores Avatar asked Sep 11 '12 14:09

FacundoGFlores


People also ask

How do you read and write vectors to a file in C++?

read((char*)&list2[0], size2 * sizeof(Vertex)); There is other stuff in the Vector data structure, so this will make your new vector get filled up with garbage. Solution: When you are writing your vector into a file, don't worry about the size your Vertex class, just directly write the entire vector into memory.

How do you read a vector?

The two defining properties of a vector, magnitude and direction, are illustrated by a red bar and a green arrow, respectively. The length of the red bar is the magnitude ∥a∥ of the vector a. The green arrow always has length one, but its direction is the direction of the vector a.

How do you write a vector file?

The simplest way to write a vector to a file is to store each element of the vector on a separate line. The above code writes each string of the vector to file text. txt. We insert “endl” at the end of each string to separate the strings from one another.


2 Answers

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)); 
like image 108
Platinum Azure Avatar answered Sep 23 '22 03:09

Platinum Azure


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])); 
like image 38
ForEveR Avatar answered Sep 26 '22 03:09

ForEveR