求教关于文件读取到程序
#include<iostream.h> #include<fstream.h>
class student
{
private:
int code;
double daily,mid,test;
double sum, sum2;
public:
student(int n) //构造函数
{
code = n;
}
void instudent() //输入平时和考试成绩,并计算总成绩
{
ofstream outfile("student.txt",ofstream::out | ofstream::app);
cout<<"Input daily score:";
cin>>daily;
cout<<"Input middle test score:";
cin>>mid;
cout<<"Input test score:";
cin>>test;
sum = daily + test + mid;
sum2=daily + test;
outfile<<"daily score:"<<daily<<endl;
outfile<<"middle test score:"<<mid<<endl;
outfile<<"final test score:"<<test<<endl;
outfile<<"sum score :"<<sum<<endl;
outfile<<"sum score2 :"<<sum2<<endl;
}
void showscore () //输出成绩
{
cout<<"\nThe student's ID:"<<code; //学生ID
cout<<"\n平时成绩 daily score:"<<daily //平时成绩
<<"\n期中成绩 middle test score:"<<mid//期中成绩
<<"\n期末成绩 final test score:"<<test//考试成绩
<<"\n总成绩1 sum score:"<<sum //总成绩1
<<"\n总成绩2 sum score:"<<sum2<<endl;//总成绩2
}
student * next;
};
void main()
{
ofstream outfile("student.txt",ofstream::out | ofstream::app);
int code;
student * head = NULL;
student * p, * q;
cout<<"输入学生ID(无则输入0结束):"; //输入学号
cin>>code;
outfile<<"student ID:"<<code<<endl;
while(code)
{
ofstream outfile("student.txt",ofstream::out | ofstream::app);
p = new student(code);
p->instudent();
if(head==NULL) head=p;
else q->next=p;
q=p;
cout<<"输入学生ID(无则输入0 结束):";
cin>>code;
outfile<<"student ID:"<<code<<endl;
}
p->next = NULL;
while(head) //输出成绩
{
head->showscore();
head = head->next;
}
}
这是我改编以后的学生类,可以支持导出任意个学生信息到student文本了,但是要如何将文本中的信息输出到屏幕?并且可以用例如 File W student.txt 类似这样的命令使文件写入到文本,而 File t student.txt 导入文本信息到文件..请求给出程序的修改..万分感谢..