Calvin W.

asked • 09/23/15

C++ open a file to get data in a format other than ASCII text

If you open up a .png file, there are characters in there which fall outside the ASCII scheme. I am looking for a way to open a file (using c++) in some binary type of format. Basically, looking to represent the data as something besides what you'd see if you changed the .png file to .txt and then opened it. Working on a program to modify the data and it works beautifully for everything found within the ASCII world but not even close to everything else.

I have tried streamName.open("filename.txt", ios::binary);. It still opens it in the text format so I ended up trying the following: streamName >> hex >> variableName; which is basically pointless if the data isn't able to be represented by ASCII characters in the first place. I have searched for days and possibly just now realized the problem... the filename as a .txt extension? If this is the case, what does it need to be to get the correct format of the data I'm looking for?

This has been a prolonged search over years (off and on) just really getting serious about it now. Any help which could be provided would be more than greatly appreciated :)

2 Answers By Expert Tutors

By:

David W. answered • 09/23/15

Experienced Prof

Calvin W.

This is by far the most helpful post I've received!  Thank you so much for that.  I basically didn't even know enough about this to cobble together an adequate search.  In addition, thank you for the warning at the end which I will need to look into.  I've been working on an encryption program since my first c++ class in '99 since it seems to incorporate so many different aspects of what I learned.  Text files (outside of microsoft documents) are extremely easy to modify.  However, when you get into the more esoteric files, it all falls apart due to unrecognizable data (outside of ASCII).
 
You've given me a great place to start and I can't thank you enough for that!
 
Calvin
Report

09/24/15

Raymond D. answered • 10/02/15

Retired, But Enjoy Passing on Knowledge

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.

OR

Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.