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

双向文件流的有关问题

2012-02-13 
双向文件流的问题#include iostream#include fstream#include stringusing namespace stdint main(

双向文件流的问题
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main(int argc, char *argv[])
{
int ii = 865876;
  fstream inoutf("test1.txt");
inoutf << "Hello Moto! " << ii << endl;

//inoutf.flush();

string str1, str2;
int ix;
inoutf >> str1 >> str2 >> ix;
cout << str1 << str2 << ix << endl;

return 0;
}

编译可以通过,但是运行时的输出是 “-858993460”,inoutf不管是否flush都是一样,不明白这种双向I/O为什么不行?


[解决办法]
inoutf.seekg(ios::beg);
inoutf >> str1 >>str2 >>ix; 
cout << str1 << str2 << ix << endl; 

还有,先确保文件test1.txt存在

热点排行