我的previous question后面出现了一个新问题:
我已经扩展了代码以执行矩阵二进制文件I / O,并且在测试简单的写入和读取操作时,我只检索了矩阵的第一行......
我没有设法找到我的错误,这是新代码:
double** bytes_to_matrix_block(std::ifstream& iF, int size1, int size2) {
double** m = new double*[size1];
double read;
int i = 0, j = 0;
if(!iF) {
std::cout << "opening file for reading error";
throw 1;
}
while(i < size1 && !iF.eof()) {
m[i] = new double[size2];
while(j < size2 && !iF.eof()) {
iF.read( reinterpret_cast<char*>( &read ), sizeof read );
m[i][j] = read;
std::cout << read << ", ";
j++;
}
std::cout << std::endl;
i++;
}
if(i < size1 || j < size2) {
std::cout << "premature end of file while reading..." << std::endl;
throw 1;
}
return m;
}
void matrix_block_to_bytes(double** m, int size1, int size2, std::ofstream& oF){
if(!oF){
std::cout << "opening file for writing error";
throw 1;
}
double cdbl;
for(int i = 0; i < size1; i++){
for(int j = 0; j < size2; j++){
cdbl = m[i][j];
std::cout << cdbl << ", ";
oF.write( reinterpret_cast<char*>( &cdbl ), sizeof cdbl );
}
std::cout << std::endl;
}
}
先谢谢
答案 0 :(得分:1)
阅读时,您忘记将j重置为0以获取新行
while(i < size1 && !iF.eof()) {
// Missing:
j = 0;