I have a char type array[100] with 100 bytes stored in it. I want to write this char type byte array to a file. How could I do this?
I am not writing to a .txt file but some other format.
Thank you.
I have a char type array[100] with 100 bytes stored in it. I want to write this char type byte array to a file. How could I do this?
I am not writing to a .txt file but some other format.
Thank you.
Some people object to using <cstdio>, so it is worth mentioning how one might use <fstream>:
{
std::ofstream file("myfile.bin", std::ios::binary);
file.write(data, 100);
}
The four lines above could be combined into this single line:
std::ofstream("myfile.bin", std::ios::binary).write(data, 100);
No need to get complicated. Just use good old fwrite directly:
FILE* file = fopen( "myfile.bin", "wb" );
fwrite( array, 1, 100, file );
ofstream-based answers provided elsewhere in this topic. It is certainly not less complicated and it is not type safe. And it contains an error (you don't close the file).