我有一个char
类型array[100]
与100
字节存储在其中。我想将这个char
类型字节数组写入文件。我怎么能这样做?
我不是写.txt
文件,而是写一些其他格式。
谢谢。
无需复杂化。只需直接使用好旧的fwrite:
FILE* file = fopen( "myfile.bin", "wb" );
fwrite( array, 1, 100, file );
有些人反对使用<cstdio>
,所以值得一提的是如何使用<fstream>
:
{
std::ofstream file("myfile.bin", std::ios::binary);
file.write(data, 100);
}
上面的四行可以组合成这一行:
std::ofstream("myfile.bin", std::ios::binary).write(data, 100);
根据您提供的(小)信息,一种可能性是将数组以二进制格式写入文件,例如:
std::ofstream out("somefile.bin", std::ios::binary);
out.write(array, sizeof(array));