【发布时间】:2012-09-25 10:12:24
【问题描述】:
我正在尝试写入/读取双矩阵到二进制数据文件,但读取时我没有得到正确的值。
我不确定这是否是使用矩阵的正确程序。
这是我用来编写它的代码:
void writeMatrixToFileBin(double **myMatrix, int rows, int colums){
cout << "\nWritting matrix A to file as bin..\n";
FILE * pFile;
pFile = fopen ( matrixOutputName.c_str() , "wb" );
fwrite (myMatrix , sizeof(double) , colums*rows , pFile );
fclose (pFile);
}
这是我用来阅读它的代码:
double** loadMatrixBin(){
double **A; //Our matrix
cout << "\nLoading matrix A from file as bin..\n";
//Initialize matrix array (too big to put on stack)
A = new double*[nRows];
for(int i=0; i<nRows; i++){
A[i] = new double[nColumns];
}
FILE * pFile;
pFile = fopen ( matrixFile.c_str() , "rb" );
if (pFile==NULL){
cout << "Error opening file for read matrix (BIN)";
}
// copy the file into the buffer:
fread (A,sizeof(double),nRows*nColumns,pFile);
// terminate
fclose (pFile);
return A;
}
【问题讨论】:
-
您是否考虑过将
std::vector<std::vector<double> >用于您的矩阵并使用boost::serialization 用于您的二进制序列化?请注意,以可移植方式二进制存储浮点数并非易事。 -
你好,现在我没有考虑使用向量库或 boost。但这可能是未来的一种选择。谢谢。
标签: c++ serialization file-io matrix binary-data