Reputation: 1338
I have a MIDI file which I am trying to read as a hex string: in particular I want to input a MIDI file and to have the hex string available for use. I have the following:
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;
When I output hexFile, I get the following (note the white space near the end):
4d546864000000060001000400f04d54726b0000001300ff58040402180800ff5103 27c000ff2f00
When I view the MIDI in a hex editor, it reads as follows:
4d546864000000060001000400f04d54726b0000001300ff58040402180800ff5103 0927c000ff2f00
The latter is definitely correct, as confirmed by track size (around the white space I manually inserted, the correct one has a 09 the former lacks).
What could have caused this 09
to go missing in my code?
Upvotes: 0
Views: 255
Reputation: 96
By default ifstream skips whitespace.
All you need to do is tell it not to.
ifstream file ("row.mid", ios::binary);
file.unsetf(ios::skipws); //add this line to not skip whitespace
Upvotes: 4
Reputation: 1338
Appending the following line after declaring ifstream file()
seems to do the trick:
file >> std::noskipws;
Upvotes: 0
Reputation: 1552
09
is an ANSII code for tabulation character. The default ofstream
mode is for text, and that's why the 09
byte is written as the actual tabulation. Try to set ios::binary
also for the output file, and it should be fine.
output.open("output.txt", ios::binary);
Upvotes: 0