C++ 使用<&书信电报;操作人员

C++ 使用<&书信电报;操作人员,c++,C++,由于某些原因,此排序代码没有像我预期的那样工作: std::fstream theFile; theFile.open(<someFilename>, std::ios::beg |std::ios::out|std::ios::binary|std::ios::trunc); theFile << 1; //1 is being written as a string int var= 25; theFile << 25;

由于某些原因,此排序代码没有像我预期的那样工作:

std::fstream theFile;
theFile.open(<someFilename>, std::ios::beg |std::ios::out|std::ios::binary|std::ios::trunc);
theFile << 1;          //1 is being written as a string
int var= 25;

theFile << 25;        //same thing, 25 is written as a string
std::fstream文件;
open(,std::ios::beg | std::ios::out | std::ios::binary | std::ios::trunc);

文件
您需要首先将值类型转换为
字符
,否则iostream库会将值视为
int
,并将其格式化为可读字符串

theFile << (char)1 << (char)25;

theFile@sehe,是的,会的。该文件不关心已签名与未签名。它将被写入未签名的文件。我鼓励你自己试试看。我就这么做了,效果很好。你可以写0到255之间的任何东西。哦…,我错了。我一定是被我的编译器允许整数也超过255的巧合弄瞎了。还有负整数,谢谢,答案很准确。显然,std::ios::binary开关仅用于优化文件查找操作;奥斯特雷姆