I just folow this :
How to write/read an Eigen matrix from binary file
namespace Eigen{
template<class Matrix>
void write_binary(const char* filename, const Matrix& matrix){
std::ofstream out(filename,ios::out | ios::binary | ios::trunc);
typename Matrix::Index rows=matrix.rows(), cols=matrix.cols();
out.write((char*) (&rows), sizeof(typename Matrix::Index));
out.write((char*) (&cols), sizeof(typename Matrix::Index));
out.write((char*) matrix.data(), rows*cols*sizeof(typename Matrix::Scalar) );
out.close();
}
template<class Matrix>
void read_binary(const char* filename, Matrix& matrix){
std::ifstream in(filename,ios::in | std::ios::binary);
typename Matrix::Index rows=0, cols=0;
in.read((char*) (&rows),sizeof(typename Matrix::Index));
in.read((char*) (&cols),sizeof(typename Matrix::Index));
matrix.resize(rows, cols);
in.read( (char *) matrix.data() , rows*cols*sizeof(typename Matrix::Scalar) );
in.close();
}
} // Eigen::
But problem is of Sparse module: When i want to write SparseVector , I gets:
void Eigen::write_binary(const char*, const Matrix&) [with Matrix = Eigen::SparseVector]’: ../re_fol/createComparisonData.cpp:54:33: instantiated from here ../re_fol/createComparisonData.cpp:33:5: error: invalid cast from type ‘const Storage {aka const Eigen::internal::CompressedStorage}’ to type ‘char*’ make: *** [re_fol/createComparisonData.o] Error 1
iN THIS line :
out.write((char*) matrix.data(), rows*cols*sizeof(typename Matrix::Scalar) );
I know that the vector is special type of matrix in eigen, but the sparse module have some may another type of sotrage data......?:
const Eigen::internal::CompressedStorage<double, > int>}’ to type ‘char*’ make: