3

私の前の質問の後に新しい問題があります:

マトリックスバイナリファイル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;
    }
}

よろしくお願いします

4

1 に答える 1

1

読むとき、新しい行のためにjを0にリセットするのを忘れています

while(i < size1 && !iF.eof()) {
// Missing:
j = 0;
于 2011-07-26T14:12:05.377 に答える