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 );
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With