I have this code snippet to write a buffer to a file
int WriteBufferToFile(std::string path, const char* buffer, int bufferSize) {
std::ofstream ofs;
ofs.open(path);
if (!ofs) {
return 1;
}
ofs.write(buffer, bufferSize);
if (!ofs) {
return 2;
}
ofs.close();
return 0;
}
Case 1
std::vector<char> buffer(1000000, 0);
WriteBufferToFile("myRawData", buffer.data(), 1000000);
Case 2
std::vector<char> buffer(1000000);
for (int i = 0; i < 1000000; i++) {
buffer[i] = char(i);
}
WriteBufferToFile("myRawData2", buffer.data(), 1000000);
In Case 1 one I'm writing 1mb of just zeros to a file, which also will have 1mb in size, but in the second case i write arbitary chars (still should be 1mb in RAM) to a file, but now (in my tests it seems like especially when char's >= 10 are contained) the file size increases.
Why is that, and is there a way to fix this?