文件读写

实现文件写入

#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;
}

结果:

posted on 2022-10-26 21:34  进取  阅读(23)  评论(0编辑  收藏  举报