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.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
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 this post


Link to post
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...

Share this post


Link to post
Share on other sites