求帮忙实现文件的保存和读取
#include<iostream>#include<cstring>
#define N 60
using namespace std;
struct student //定义了一个结构体
{
char Name[20];
char Number[20];
char CourseTitle[20];
int CourseNo;
int CreditHours;
int mark[10];
float score;
};
struct student stu[N];
void studentcourse()//实现任意学生数的学分积点计算
{
int count1;
int count2;
float T1=0,T2=0;float T3;
cout<<"请输入学生的个数"<<endl;
cin>>count2;
cout<<"请输入科目的个数"<<endl;
cin>>count1;
for( int j = 0;j < count1;j ++)
{
cout<<"请输入第"<<j+1<<"课课程名:"<<endl;
cin>>stu[j].CourseTitle;
cout<<"请输入第"<<j+1<<"课课程号:"<<endl;
cin>>stu[j].CourseNo;
cout<<"请输入第"<<j+1<<"课课程的学分数:"<<endl;
cin>>stu[j].CreditHours;
}
cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;
for(j=0;j<count2;j++)
{
cout<<"请输入第"<<j+1<<"个学生的姓名"<<endl;
cin>>stu[j].Name;
cout<<"请输入第"<<j+1<<"个学生的学号"<<endl;
cin>>stu[j].Number;
cout<<endl;
for(int i=0;i<count1;i++)
{
cout<<"请输入该生"<<stu[i].CourseTitle<<"的成绩:";
cin>>stu[j].mark[i];
}
cout<<endl;
for( i =0; i < count1; i++)
{
T1 += (stu[j].mark[i]-50)*stu[i].CreditHours/10;
T2 +=stu[i].CreditHours;
}
stu[j].score = T1/T2;
}
cout<<"\tName\tNumber\t";
for(int i = 0; i < count1; i++)
{
cout<<stu[i].CourseTitle<<"\t";
}
cout<<"score"<<endl;
for( j = 0; j < count2; j++)
{
cout<<"\t"<<stu[j].Name<<"\t"<<stu[j].Number<<"\t";
for(int i = 0; i < count1; i++)
{
cout<<stu[j].mark[i]<<"\t";
}
cout<<stu[j].score<<endl;
}
}
void Sort()// 用来按学分积点的大小来排序
{
int i,j;
float temp;
for (i=0;i<n;i++)
{
for (j=1;j<n-i;j++)
{
if ((stu[j-1].score -stu[j].score)>0)
{
temp=stu[j-1].score;
stu[j-1].score=stu[j].score;
stu[j].score=temp;
}
}
}
for(i=0;i<n;i++){cout<<stu[i].score<<endl;}
}
void menu()
{
cout<<"*****************************学分积点计算系统*****************************"<<endl<<endl<<endl;
cout<<" \t\t "<<"1.任意学生数的学分计算"<<"\t\t"<<"2.读取学生学分积点信息"<<endl<<endl;
cout<<" \t\t "<<"3.保存学生学分积点信息"<<"\t\t"<<"4.返回主菜单"<<endl<<endl;
cout<<"请输入你要进行的操作:"<<endl;
}
void main()
{
int m=0,n;
while(m==0)
{
menu();
cin>>n;
switch(n)
{
case 1:studentcourse();break;
case 2:coursecourse();break;
case 3:Sort();break;
case 4:m=1;break;
}
}
}
想通过文件的保存和读写,在计算完学分积点后,把信息保存下来,然后在主函数中直接调用sort()函数来排序,可是ofstream函数不明白怎么用在这个程序上,谁能帮我把文件的保存和读写加上,不胜感激!