C++ 如何使用.read继续读取二进制文件?

C++ 如何使用.read继续读取二进制文件?,c++,loops,file-io,binary,C++,Loops,File Io,Binary,我试图读取一个二进制文件,其中包含一个1501乘1501的双精度矩阵,并将其插入一个特征矩阵。这是我的密码: #include <fstream> #include <iostream> #include <Eigen/Dense> #include <string> using namespace std; using namespace Eigen; int main() { MatrixXd B(1501, 1501);

我试图读取一个二进制文件,其中包含一个1501乘1501的双精度矩阵,并将其插入一个特征矩阵。这是我的密码:

#include <fstream>
#include <iostream>
#include <Eigen/Dense>
#include <string>

using namespace std;
using namespace Eigen;

int main()
{
    MatrixXd B(1501, 1501);
    ifstream inputFile;

    double toread;

    inputFile.open("/path/to/bathymetry_S1000s2500s_E65d1000s65d2500s.bin",
                   ios::out | ios::in | ios::binary);
    if (!inputFile) {
        cout << "The file can't be opened.\n";
        exit(10);
    } else {    
        for (int i2 = 0; i2 < 1501; i2++) {
            for (int i1 = 0; i1 < 1501; i1++) {
                inputFile.read( reinterpret_cast<char*>( &toread ),
                                sizeof(toread) );
                inputFile >> toread;
                B(i1, i2) = toread;
            }

        }
        inputFile.close();
    }

    cout << "Max value:" << B.maxCoeff() << endl; // Just to check the result
    cout << "Mean Value:" << B.mean() << endl; // The same
}
#包括
#包括
#包括
#包括
使用名称空间std;
使用名称空间特征;
int main()
{
MatrixXd B(15011501);
ifstream输入文件;
双探路者;
inputFile.open(“/path/to/dethymetry_S1000s2500s_E65d1000s65d2500s.bin”,
ios::out | ios::in | ios::binary);
如果(!inputFile){
库特>探路者;
B(i1,i2)=探路者;
}
}
inputFile.close();
}

cout Wat是
inputFile>>toread用于?(不要混合格式化和未格式化输入)您没有检查
inputFile.read()的结果
调用inputFile>>toread是用来将最近读取的值插入到一个双变量中,然后插入到矩阵B中的。否则您会怎么做?因为我没有收到任何编译错误,并且读取文件时会出现一些问题,所以我没有考虑检查inputFile.read()调用的结果。我认为问题不在于它读不读,问题在于它如何读。谢谢你们的回答,错误已经被发现了。删除inputFile>>toread行,一切正常。我确实复制/粘贴了那部分代码,显然这不是一个好主意。