Archived

This topic is now archived and is closed to further replies.

ofstream

This topic is 4990 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

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

Share on other sites
If you write ascii characters in hex, doesn''t text editor display them as ascii characters?

Share on other sites
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.

Share on other sites
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]);

Share on other sites
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...

• Forum Statistics

• Total Topics
628751
• Total Posts
2984503

• 12
• 25
• 12
• 10
• 17