编写程序,用户输入10个学生的成绩信息,包括学号,姓名,英语成绩,数学成绩和计算机成绩

将用户的信息保存到文件中,并支持查找,追加功能 用c语言知识回答

第1个回答  2014-12-08
很简单啊

#include <stdio.h>
#incluude <fstream>
#include <iostream>
#include<string>
using namespace std;
void find(ofstream &fp, char * szKey)
{
ifstream in("grades.txt");
if(!in)
{
cerr << "some errors happened";
return -1;
}
string str;
while(getline(in, str))
{
if(-1!=str.find_first_of(szKey))
{
cout << str << endl;
break;
}
}
return 0;
}
void append(ofstream &fp)
{
char szNo[16];
char szName[10];
float fEnglish, fMath, fComputer;
//输入中间用空格间隔
scanf("%s %s %f %f %f\n",szNo, szName, &fEnglish, &fMath, &fComputer);
fp << szNo << szName << fEnglish << fMath << fComputer<<endl;
}

int main()
{
ofstream resulttxt;
resulttxt.open("grades.txt", ios_base::out | ios_base::app);
resulttxt << "学号||" << "姓名||" << "英语成绩||" << "数学成绩||" << "计算机成绩"<<endl;
for(int i = 0; i < 10; i++)
{
append(resulttxt);
}
resulttxt.close();
return 0;
}本回答被网友采纳
相似回答