使用<<运算符将二进制文件写入std :: fstream

seg*_*man 5 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;        //same thing, 25 is written as a string
Run Code Online (Sandbox Code Playgroud)

可能是什么问题呢?我正在使用Visual Studio 2010附带的Microsoft C++编译器.

fsc*_*enm 8

<<运营商的整个目的是格式化的数据写入流.如果要编写二进制数据,则应使用ostream::write()ostream::put().