Jump to content
  • Advertisement
Sign in to follow this  
kpanghmc

Writing using ofstream

This topic is 4656 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

Hi, I have a buffer of unsigned chars that I would like to write to disk (and later be able to read) Currently, my code looks something similar to this: // Dynamically allocate memory for buffer unsigned char* buffer = new unsigned char[buffer_length]; // Add and manipulate buffer data ... // Write buffer to disk std::ofstream output; output.open("pathname"); output.write(buffer, buffer_length); output.close(); Now, since write() only supports char* and not unsigned char*, could I get away with doing a cast to char* in the write statement like this: output.write((char*)buffer, buffer_length); or would there be a loss in data there? If there is a loss of data, can anyone think of a good way to get around this? Thanks in advance!

Share this post


Link to post
Share on other sites
Advertisement
reinterpret_cast<char*>(buffer) will reinterpret the pointer without modifying the data. You will not lose anything.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!