2013-10-29 47 views
0

我有一個MIDI文件,我試圖讀取爲十六進制字符串:特別是我想輸入一個MIDI文件並使用十六進制字符串可供使用。我有以下幾點:作爲十六進制字符串讀取MIDI文件:缺少一些信息

ostringstream ss; 
char * memblock; 
unsigned char x; 
std::string hexFile; 

ifstream file ("row.mid", ios::binary); 
ofstream output; 
output.open("output.txt"); 

while(file >> x){ 
    ss << hex << setw(2) << setfill('0') << (int) x; 
} 

hexFile = ss.str(); 
cout << hexFile; 

當我輸出hexFile,我得到以下(注意接近尾聲的空格):

4d546864000000060001000400f04d54726b0000001300ff58040402180800ff5103 27c000ff2f00 

當我在十六進制編輯器查看MIDI,它讀取如下:

4d546864000000060001000400f04d54726b0000001300ff58040402180800ff5103 0927c000ff2f00 

後者是絕對正確的,因爲由軌道尺寸確認(我周圍手動插入白空間,正確的一個具有09前者缺乏)。

什麼可能導致此09在我的代碼中失蹤?

回答

4

默認情況下ifstream的跳過空白。
所有你需要做的就是告訴它不要。

ifstream file ("row.mid", ios::binary); 
file.unsetf(ios::skipws); //add this line to not skip whitespace 
0

09是製表符的ANSII代碼。默認ofstream模式用於文本,這就是爲什麼09字節被寫爲實際製表符。嘗試設置ios::binary也爲輸出文件,它應該沒問題。

output.open("output.txt", ios::binary); 
+0

我不使用ofsteam(在此代碼段中,理想情況下稍後將字符串寫入output.txt)。我正在打印實際的字符串,它缺少09; – mike

0

宣佈追加後ifstream file()以下行,似乎這樣的伎倆:

file >> std::noskipws; 
相關問題