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