简单的文件录入与读出
录入:
#include <iostream>
#include<string>
#include<fstream>
using namespace std;
void main()
{
ofstream outfile("D:\\student.dat");//建立输出数据文件
string name,id;
int math, eng, computer;
for (int i = 0; i < 3; i++) {
cout << "输入姓名"; cin >> name;
cout << "输入学号"; cin >> id;
cout << "输入数学成绩"; cin >> math;
cout << "输入英语成绩"; cin >> eng;
cout << "输入计算机成绩"; cin >> computer;
outfile << name << "" << id << "" << math << "" << eng << ""<<computer << endl;//将数据录入文件夹中
}
outfile.close();//关闭文件
}
读
#include <iostream>
#include<fstream>
#include<string>
using namespace std;
void main()
{
ifstream infile("D:\\student.dat");//找到文件
string name,id;
int math, eng, computer;
for (int i = 0; i < 3; i++) {
cout << "姓名\t" << "学号\t" << "数学成绩\t" << "英语成绩\t" << "输入计算机成绩\t" << "总分" << endl;
infile >> name;
while (!infile.eof()) {
infile >> id >> math >> eng >> computer;
cout<< name << "" << id << "" << math << "" << eng << "" << computer
<<""<<eng+math+computer<<endl;//将数据输出显示出来
infile >> name;
}
}
infile.close();//关闭文件
}