实现文件写入
#include<iostream>
#include<fstream>
using namespace std;
void main()
{
fstream wfs("test5.txt", ios::out);
if(!wfs)
cout << "打开文件失败" << endl;
else
{
wfs << "姓名: 张三" << endl;
wfs << "性别: 男" << endl;
wfs << "年龄: 20" << endl;
}
wfs.close();
}
实现文件读取
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
fstream infile;
infile.open("test5.txt",ios::in);
if(!infile)
{
cout << "文件打开失败" << endl;
return 0;
}
char ch;
while(ch!=EOF)
{
ch = infile.get();
cout << ch ;
}
cout << endl;
infile.close();
return 0;
}
结果:
标签:文件,txt,test5,cout,读写,include,infile From: https://www.cnblogs.com/littleboss/p/16830161.html