1
我嘗試通過下面的C++程序讀取二進制數據。但它不能顯示值。 The data保存爲8位無符號字符。讓我知道如何解決它。通過C++讀取二進制數據
#include <iostream>
#include <fstream>
using namespace std;
int main(int argc,char *argv[])
{
if(argc!=2)
{
cout << "argument error" << endl;
return 1;
}
ifstream file (argv[1], ios::in|ios::binary);
//ifstream fin(outfile, ios::in | ios::binary);
if (!file)
{
cout << "Can not open file";
return 1;
}
unsigned char d;
while(!file.eof())
{
file.read((char *) &d, sizeof(unsigned char));
cout << d << endl;
}
file.close();
return 0;
}
正如您所說,該值可以由固定程序顯示0和1。我想在8位顯示0和255。讓我知道如何去做。 – LenItsuki
@LenItsuki我不確定你的意思是...你的意思是說,輸入文件中的每個*字節只能是單個'1'或'0'?你可以編輯你的問題,包括輸入文件的部分十六進制轉儲(不,沒有人會下載並打開一個未知文件,如你鏈接到的)。 –
我弄錯了文件中包含的值。 0和1都OK!感謝您的建議。 – LenItsuki