使用<<将二进制文件写入std :: fstream。算子 [英] Writing binary to std::fstream using the << operator

查看:158
本文介绍了使用<<将二进制文件写入std :: fstream。算子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于某种原因,这种排序代码无法正常工作:

For some reason this sort code is not working as I would expect:

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

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

What could be the problem? I am using the Microsoft C++ compiler that ships with Visual Studio 2010.

推荐答案

<< 运算符的全部目的是将格式化的数据写入流中。如果要写入二进制数据,则应使用 ostream :: write() ostream :: put()

The << operator's whole purpose is to write formatted data to a stream. If you want to write binary data, you should use ostream::write() or ostream::put().

这篇关于使用&lt;&lt;将二进制文件写入std :: fstream。算子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆