c++ ifstream ofstream的用法

// BASIC.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>
#include <fstream>
using namespace std;


int main()
{
	ifstream infile;
	ofstream outfile;
	char name[20];
	char str[30];
	cout << "请输入文件" << "\n";
	cin >> name;
	outfile.open(name);
	if (!outfile)
	{
		cout << "写文件打开失败";
		exit(1);
	}
	cout << "请输入要写入文件的数据";
	cin >> str;
	outfile << str;
	outfile.close();
	infile.open(name);
	if (!infile)
	{
		cout << "读文件打开失败";
		exit(1);
	}
	char c;
	cout << "输出文件中数据" << "\n";
	while (infile.get(c))
	{
		cout << c;
	}
	cout << "\n";
	infile.close();



}

猜你喜欢

转载自blog.csdn.net/dxm809/article/details/114569192