ofstream

Started by
3 comments, last by Instruo 20 years ago
I''m have some issues getting fstream to output a file in hex. I''ve tried both of these setups (plus a million others, it seems): ofstream ofile; string buffer; ofile.open (filename.c_str(), ios::binary); buffer = "Test Text\n"; ofile << hex; ofile.write(buffer.c_str(), buffer.length()); ofile.close() and: ofstream ofile; string buffer; ofile.open (filename.c_str(), ios::binary); buffer = "Test Text\n"; ofile.setf(ios::hex); ofile.write(buffer.c_str(), buffer.length()); ofile.close() These both output: Text Test in plain text... it bothers me, ''cause at some points during my fumblings I got it to output as hex. Any ideas as to how I can fix this would be very greatly appreciated. Thanks! Brian
"Game Programming" in an of itself does not exist. We learn to program and then use that knowledge to make games.
Advertisement
If you write ascii characters in hex, doesn''t text editor display them as ascii characters?
Well, I was trying to read them in VS .Net (which showed them to me in Hex mode at a couple of points). Also tried it with numbers and it just shows up as text.
"Game Programming" in an of itself does not exist. We learn to program and then use that knowledge to make games.
I believe that you need something like:
ofstream f(filename.c_str());f << setw(2) << hex;for (int i = 0; i < buffer.size(); ++i)    f << (int)(buffer[i]);
quote:
buffer = "Test Text\n";ofile.setf(ios::hex);ofile.write(buffer.c_str(), buffer.length()); 



The ios::hex manipulator applies to numbers only (think about it). You are feeding the stream a const char*.
You should instead feed it each and every character of your string, casted to int, like Beer Hunter suggests...

This topic is closed to new replies.

Advertisement