首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

C++怎么读写二进制文件

2012-04-26 
C++如何读写二进制文件#include fstream#include iostreamusing namespace stdvoid main(){char *fil

C++如何读写二进制文件
#include <fstream>
#include <iostream>
using namespace std;
void main()
{
char *fileName = "1.dat";
ofstream os(fileName, ofstream::binary);
short b = 128;
os << b ;
ifstream is(fileName, ifstream::binary);
short a;
is >> a;
cout << a << endl;
}

为什么输出是-13108

[解决办法]
需要刷新缓冲区

C/C++ code
os << b ;os.flush();ifstream is(fileName, ifstream::binary);short a;is >> a;cout << a << endl; 

热点排行