我有一个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在我的代码中丢失?

最佳答案

默认情况下,ifstream跳过空格。
您需要做的就是告诉它不要。

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

10-06 10:00