参考学习地址:C++ 文件和流
char data[100];
//以写的模式打开文件
ofstream outFile;
cout << "Writing to the file" << endl;
cout << "Enter your name: ";
outFile.open("afile.dat");
cin.getline(data,100);
outFile << data << endl;
cout << "Enter your age: ";
cin >> data;
cin.ignore();
//再次向文件写入用户输入数据
outFile << data <<endl;
//关闭打开的文件
outFile.close();
//以读的模式打开文件
ifstream infile;
infile.open("afile.dat");
cout << "reading from the file" << endl;
infile >> data;
// 在屏幕上写入数据
cout << data << endl;
// 再次从文件读取数据,并显示它
infile >> data;
cout << data <<endl;
// 关闭打开的文件
infile.close();
cout << "end!!" << endl;
作为学习笔记以后复习查询方便!