我想讀這個二進制文件並在屏幕上打印數字,但它打印奇怪的字符。我從MATLAB生成這個二進制文件。我如何正確顯示數據?閱讀二進制文件
#include <iostream>
#include <fstream>
using namespace std;
ifstream::pos_type size;
char * memblock;
int main()
{
ifstream file ("seg.bin", ios::in|ios::binary|ios::ate);
if (file.is_open())
{
size = (int)file.tellg();
memblock = new char [size];
file.seekg (0, ios::beg);
file.read (memblock, size);
file.close();
cout << "the complete file content is in memory";
for (int i=0;i<size;i++)
{
cout<<memblock[i]<<endl;
}
}
else cout << "Unable to open file";
return 0;
}
這樣做:'cout <<(int)memblock [i] << endl;' – sgarizvi
@ sgar91如果原始數據的類型爲「char」,那麼只會這樣做。 OP沒有告訴我們數據類型是什麼。 – paddy
請記得刪除後面的[] memblock,以防止內存泄漏 – Enigma