#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
string s;
//ifstream ifs("file1.txt", ifstream::in);//这是文件模式,
ifstream ifs("file1.txt"); // 这是默认的文件模式,
ifs >> s;
ifs.close();
cout << s << endl;
ofstream ofs("file2.txt",ofstream::out); // 文件输出流,ofstream::trunc将原来的文件全部清除再将新的文件输入进去,
ofstream ofs4("file1.txt", ofstream::out | ofstream::app);//ofstream::app将文件追加到文件夹里边,
ofs4 << "hello file4 " << endl;
ofs4.close();
//fstream fs("file1.txt");
fstream fs("file1.txt",fstream::in | fstream::out);//单独的使用一个out就会把文件清空,和in一起使用就不会把文件清空,把文件内容清空再加上trunc,
fs >> s;
fs.close();
cout << s << endl;
return 0;
}
文件流对象的使用
最新推荐文章于 2022-12-10 20:20:30 发布