fstream类的用法 求教
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
fstream fs ("pos.txt",ios::in | ios::out);
fs << 123 << " " << 1.23 << " " << "hello" << "\n" ;
return 0;
}
求大家指教,为啥我写完上述代码的时候,没有在目录中建立pos.txt啊
是少写代码了吗
[解决办法]
一般不推荐C++中的stream,不如c原来来到简单,也不如直接调用系统api来的高效。
[解决办法]
那个文件流应该关闭
#include<iostream>
#include <fstream>
#include <string>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
fstream fin ("pos.txt", ios::in | ios::out);
if(!fin){
cout << "Failure" << endl;
return -1;
}
fin << 123 << " " << 1.23 << " " << "hello" << "\n" ;
fin.close();
fstream fout ("pos.txt", ios::in | ios::out);
if(!fout){
cout << "Failure" << endl;
return -1;
}
int a;
double b;
string str1;
fout >> a;
fout >> b;
fout >> str1;
cout << a << endl;
cout << b << endl;
cout << str1 << endl;
fout.close();
return 0;
}
这样能实现楼主所需要的功能,但是感觉怪怪的。建议楼主还是将文件输入流与输出流隔离开来。