我有 char 数组,我想将它写入 txt 文件,但以字节为单位。
ofstream sw("C:\\Test.txt");
for(int i = 0; i < 256; i++)
{
sw << (byte)myArray[i];
}
这会将字符写入文件,但我想写入字节。如果有字符 'a' 我想写 '97'。谢谢。
最佳答案
要使用 std::fstream 或 std::ofstream 写入一个字节或一组字节,您可以使用 write()
函数:std::ostream::write ()
const int ArraySize = 100;
Byte byteArray[ArraySize] = ...;
std::ofstream file("myFile.data");
if(file)
{
file.write(&byteArray[0], ArraySize);
file.write(&moreData, otherSize);
}
关于c++ - 在 C++ 中将字节写入 ofstream,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10165938/