读书人

双向文件流的有关问题

发布时间: 2012-02-13 17:20:26 作者: rapoo

双向文件流的问题
#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存在

读书人网 >C++

热点推荐