使用 fwrite/fread 对矩阵进行二进制(反)序列化不起作用
Binary (de-)serialization of a matrix using fwrite/fread doesn't work
我正在尝试将双矩阵写入/读取为二进制数据,但在读取时没有得到正确的值。
我不确定这是否是使用矩阵的正确过程。
这是我用来编写它的代码:
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;
}
它
不起作用,因为myMatrix
不是一个单一的连续内存区域,它是一个指针数组。你必须在循环中写入(和加载):
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" );
for (int i = 0; i < rows; i++)
fwrite (myMatrix[i] , sizeof(double) , colums , pFile );
fclose (pFile);
}
阅读时类似。
相关文章:
- 我的神经网络不起作用 [XOR 问题]
- 为什么二进制搜索在我的测试中不起作用
- “std::count_if”的二进制谓词不起作用
- 为什么此基准代码用于线性和二进制搜索不起作用
- 将数组保存到二进制文件不起作用 c++
- 二进制树级别明智的打印功能不起作用
- 二进制树数算法不起作用
- 二进制搜索功能不起作用
- 当我在Windows上运行它时,为什么我的Linux编译二进制不起作用
- 计算 c# 中二进制文件符号的频率不起作用,但适用于等效的 c++ 代码
- 二进制文件:ios ::二进制文件不起作用
- 插入二进制树(非BST)不起作用
- 非递归二进制树插入()方法不起作用
- 使用 fwrite/fread 对矩阵进行二进制(反)序列化不起作用
- 以二进制模式写入文件不起作用
- 二进制搜索树递归插入导致堆栈溢出,迭代插入不起作用
- 用于读写的二进制流C++不起作用
- 提升二进制序列化偶尔不起作用。解析的数据有时会损坏
- 二进制搜索树--删除函数不起作用
- C++二进制树构建为什么不起作用