fstream文件流的读与写-demo

#include 
#include 
#include 
using namespace std;

int main()
{
	string data;

	// 以写模式打开文件
	ofstream outfile;
	outfile.open("E:/wt/tet.txt", ios::app); //app为了保留上次写好的的数据

	cout << "Writing to the file" << endl;
	cout << "Enter your name: ";
	getline(cin,data);
	outfile << data << endl;// 向文件写入用户输入的数据
	cout << "Enter your age: ";
	cin >> data;
	cin.ignore();
	outfile << data << endl;// 再次向文件写入用户输入的数据

	// 关闭打开的文件
	outfile.close();
	//==============================================================

	// 以读模式打开文件
	ifstream infile;
	infile.open("E:/wt/tet.txt");
	cout << "Reading from the file" << endl;
	while (infile >> data)
	{
		// 在屏幕上写入数据
		cout << data << endl;
	}

	// 关闭打开的文件
	infile.close();

	system("pause");
	return 0;
}

 

你可能感兴趣的:(C++备注笔记,c++)