第一步:构建一个头文件(**.h)
#include<iostream>
#include<iomanip>
#include<fstream>
#include<string>
using namespace std;
class CStudentRec
{
public:
char chFlag;//标志,A表示正常,N表示空
char strName[20];//姓名
char strID[10];//学号
float fScore[3];//3门成绩
float fAve;//总平均分
CStudentRec(char *name,char *id,float score[]);
CStudentRec(){chFlag='N';};//默认构造函数
~CStudentRec(){};//默认析构函数
void Input(void);
float Validate(void);//成绩数据的输入验证,返回正确值
void Print(bool isTitle=false);
friend ostream& operator<<( ostream& os,CStudentRec& stu);
friend istream& operator>>( istream& is,CStudentRec& stu);
};
CStudentRec::CStudentRec(char *name,char *id,float score[])
{
strncpy(strName,name,20);
strncpy(strID,id,10);
fAve=0;
for(int i=0;i<3;i++)
{
fScore[i]=score[i];
fAve+=fScore[i];
}
fAve=float(fAve/3.0);
chFlag='A';
}
void CStudentRec::Input(void)
{
cout<<"姓名:";
cin>>strName;
cout<<"学号:";
cin>>strID;
float fSum=0;
for(int i=0;i<3;i++)
{
cout<<"成绩"<<i+1<<":";
fScore[i]=Validate();
fSum+=fScore[i];
}
fAve=(float)(fSum/3.0);
chFlag='A';
getchar();
}
float CStudentRec::Validate(void)
{
int s;
char buf[80];
float res;
for(;;)
{
cin>>res;
s=cin.rdstate();
while(s)
{
cin.clear();
cin.getline(buf,80);
cout<<"非法输入,重新输入:";
cin>>res;
s=cin.rdstate();
}
if((res<=100.0)&&(res>=0.0))break;
else
cout<<"输入的成绩超过范围!请重新输入:";
}
return res;
}
void CStudentRec::Print(bool isTitle)
{
cout.setf(ios::left);
if(isTitle)
cout<<setw(20)<<"姓名:"<<setw(10)<<"学号:"<<"\t成绩1:"<<"\t成绩2:"<<"\t成绩3:"<<"\t平均分:"<<endl;
cout<<setw(20)<<strName<<setw(10)<<strID;
for(int i=0;i<3;i++)
cout<<"\t"<<fScore[i];
cout<<"\t"<<fAve<<endl;
getchar();
}
ostream& operator<<(ostream& os,CStudentRec& stu)
{
os.write(&stu.chFlag,sizeof(char));
os.write(stu.strName,sizeof(stu.strName));
os.write(stu.strID,sizeof(stu.strID));
os.write((char*)&stu.fScore,sizeof(float)*3);
os.write((char*)&stu.fAve,sizeof(float));
return os;
}
istream& operator>>(istream& is,CStudentRec& stu)
{
char name[20],id[10];
is.read(&stu.chFlag,sizeof(char));
is.read(name,sizeof(name));
is.read(id,sizeof(id));
is.read((char*)stu.fScore,sizeof(float)*3);
is.read((char*)&stu.fAve,sizeof(float));
strncpy(stu.strName,name,sizeof(name));
strncpy(stu.strID,id,sizeof(id));
return is;
}
class CStuFile
{
public:
CStuFile(char* filename);
~CStuFile();
void Add(CStudentRec stu);
int Seek(char* id,CStudentRec &stu);
int List(int nNum=-1);
private:
char* strFileName;
};
CStuFile::CStuFile(char* filename)
{
strFileName=new char[strlen(filename)+1];
strcpy(strFileName,filename);
}
CStuFile::~CStuFile()
{
if(strFileName)delete []strFileName;
}
void CStuFile::Add(CStudentRec stu)
{
fstream file(strFileName,ios::out|ios::app|ios::binary);
file<<stu;
file.close();
}
int CStuFile::Seek(char* id,CStudentRec& stu)
{
int nRec=-1;
fstream file(strFileName,ios::in|ios::_Nocreate);
if(!file)
{
cout<<"文件"<<strFileName<<"不能打开!\n";
return nRec;
}
int i=0;
while(!file.eof())
{
file>>stu;
if((strcmp(id,stu.strID)==0)&&(stu.chFlag!='N'))
{
nRec=i;break;
}
i++;
}
file.close();
return nRec;
getchar();
}
int CStuFile::List(int nNum)
{
fstream file(strFileName,ios::in|ios::_Nocreate);
if(!file)
{
cout<<"文件"<<strFileName<<"不能打开!\n";
return 0;
}
int nRec=0;
if((nNum==-1)||(nNum>0))
{
cout.setf(ios::left);
cout<<setw(6)<<"记录"<<setw(20)<<"姓名"<<setw(10)<<"学号"<<"\t成绩1\t成绩2\t成绩3\t平均分"<<endl;
}
while(!file.eof())
{
CStudentRec data;
file>>data;
if(data.chFlag=='A')
{
nRec++;
if((nNum==-1)||(nRec<=nNum))
{
cout.setf(ios::left);
cout<<setw(6)<<nRec;
data.Print();
}
}
}
file.close();
return nRec;
}
第二部:创建一个.cpp文件
#include<iostream>
#include"**.h"
CStuFile theStu("student.txt");
void AddTo(int nNum)
{
CStudentRec stu;
for(int i=0;i<nNum;i++)
{
cout<<"请输入第"<<i+1<<"记录:"<<endl;
stu.Input();
theStu.Add(stu);
}
}
void main()
{
AddTo(3);
theStu.List();
CStudentRec one;
if(theStu.Seek("21050102",one)>=0)
one.Print(true);
else
cout<<"没有找到!\n";
theStu.List();
getchar();
}
ok