必须包含头文件
#include<fstream>
三种方式
ifstream 读文件
ofstream 写文件
fstream 读写文件
//创建流对象
fstream iof;//iof对象名
iof.open("路径名",打开方式);
//关闭文件
iof.close();
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
int main()
{
fstream iof;
iof.open("E:/程序代码/文件/文件/fstream.txt",ios::out| ios::in);
if (iof.is_open()) //is_open判断文件打开是否成功
{
// 文件写
// cout << "请输入内容:" << endl;
// string x;
// cin >> x;
// iof << x << endl;
// 文件读
// 第一种
// char y[1024] = {0};
// while (iof >> y)
// {
// cout << y << endl;
// }
// 第二种
// char y[1024] = { 0 };
// while (iof.getline(y,sizeof(y)))
// {
// cout << y << endl;
// }
// 第三种
string y;
getline(iof, y);
cout << y << endl;
// 文件关闭
iof.close();
}
else
{
cout << "没有这个文件" << endl;
}
system("pause");
return 0;
}