バイナリ ファイルにデータを書き込んでから、ファイルからデータを読み戻そうとしています。データは単一の数値 (整数) nrows で構成されます。以下は、データをバイナリ ファイルに書き込むための私のコードです。しかし、データを読み戻して printf を使用して結果を出力しようとすると、無意味な結果 -2 が得られます。
FILE *fout;
FILE *file_pointer;
int nrows = 5;
fout = fopen("matrixB.bin", "wb") //Writing to a binary file.//
fwrite(&nrows, sizeof(int), 1, fout); //Writing the number nrows to the binary file "matrixB.bin"//
file_pointer = fopen("matrixB.bin", "rb"); //Reading a binary file.//
fread(&nrows, sizeof(int), 1, file_pointer);
printf("%d", nrows); //Here -2 is printed, instead of 5.//
私のコードの問題は何ですか?