• Advertisement
Sign in to follow this  

Writing using ofstream

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