Writing using ofstream
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!
reinterpret_cast<char*>(buffer) will reinterpret the pointer without modifying the data. You will not lose anything.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement