1

ダブルマトリックスバイナリデータとしてファイルに書き込み/読み取りしようとしていますが、読み取り時に正しい値を取得できません。

これが行列でそれを行うための正しい手順であるかどうかはわかりません。


これが私がそれを書くために使っているコードです:

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;
}
4

1 に答える 1

2

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);
}

読んでいるときも同様です。

于 2012-09-25T10:21:07.617 に答える