1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
   | #include <iostream> #include <fstream>
  using namespace std;
  int main() { 	string name, school; 	int age; 	ofstream fout("test.txt", ios::out | ios::trunc);	 	cout << "姓名: "; 	cin >> name; 	cout << "年龄: "; 	cin >> age; 	cout << "学校: "; 	cin >> school; 	fout << name << endl; 	fout << age << endl; 	fout << school << endl; 	cout << "信息已写入文件!" << endl; 	fout.close();	
  	ifstream fin;	 	cout << "\n读取文件..." << endl; 	fin.open("test.txt", ios::in);	 	fin >> name; 	fin >> age; 	fin >> school; 	fin.close();	
  	cout << "文件内容如下:" << endl; 	cout << "姓名: " << name << endl; 	cout << "年龄: " << age << endl; 	cout << "学校:" << school << endl;
  	return 0; }
   |