P
polestar
#include <fstream.h>
#include <string.h>
class TStu
{
private:
int Number;
char Name[20];
int Age;
char Sex[2];
public:
TStu();
TStu(int num,const char *n,int a,char *s);
int get()
{
return Number;
}
friend istream& operator>>(istream& s,TStu& n);
friend ostream& operator<<(ostream& s,TStu& n);
};
TStu::TStu()
{
Number=0;
Name[0]=0;
Age=0;
Sex[0]=0;
}
TStu::TStu(int num,const char *n,int a,char *s)
{
Number=num;
strcpy(Name,n);
Age=a;
strcpy(Sex,s);
}
istream& operator>>(istream& s,TStu& n)
{
s.read((char *)(&n.Number),sizeof(n.Number));
s.read(n.Name,sizeof(n.Name));
s.read((char *)(&n.Age),sizeof(n.Age));
s.read(n.Sex,sizeof(n.Sex));
return s;
}
ostream& operator<<(ostream& s,TStu& n)
{
s.write((char *)(&n.Number),sizeof(n.Number));
s.write(n.Name,sizeof(n.Name));
s.write((char *)(&n.Age),sizeof(n.Age));
s.write(n.Sex,sizeof(n.Sex));
return s;
}
void main()
{
ifstream in("student.txt");
ofstream o("student.txt");
int n;
cout<<"input the number of students:";
cin>>n;
TStu s1,s2;
for(int i=0;i<n;i++)
{
cin>>s1;
o<<s1;
}
int xnum;
cout<<"input the xnum:";
cin>>xnum;
while(1)
{
if(in.eof())
{
cout<<"not find!"<<endl;
break;
}
in>>s2;
if(s2.get()!=xnum)
continue;
else
{
cout<<s2;
break;
}
}
}
#include <string.h>
class TStu
{
private:
int Number;
char Name[20];
int Age;
char Sex[2];
public:
TStu();
TStu(int num,const char *n,int a,char *s);
int get()
{
return Number;
}
friend istream& operator>>(istream& s,TStu& n);
friend ostream& operator<<(ostream& s,TStu& n);
};
TStu::TStu()
{
Number=0;
Name[0]=0;
Age=0;
Sex[0]=0;
}
TStu::TStu(int num,const char *n,int a,char *s)
{
Number=num;
strcpy(Name,n);
Age=a;
strcpy(Sex,s);
}
istream& operator>>(istream& s,TStu& n)
{
s.read((char *)(&n.Number),sizeof(n.Number));
s.read(n.Name,sizeof(n.Name));
s.read((char *)(&n.Age),sizeof(n.Age));
s.read(n.Sex,sizeof(n.Sex));
return s;
}
ostream& operator<<(ostream& s,TStu& n)
{
s.write((char *)(&n.Number),sizeof(n.Number));
s.write(n.Name,sizeof(n.Name));
s.write((char *)(&n.Age),sizeof(n.Age));
s.write(n.Sex,sizeof(n.Sex));
return s;
}
void main()
{
ifstream in("student.txt");
ofstream o("student.txt");
int n;
cout<<"input the number of students:";
cin>>n;
TStu s1,s2;
for(int i=0;i<n;i++)
{
cin>>s1;
o<<s1;
}
int xnum;
cout<<"input the xnum:";
cin>>xnum;
while(1)
{
if(in.eof())
{
cout<<"not find!"<<endl;
break;
}
in>>s2;
if(s2.get()!=xnum)
continue;
else
{
cout<<s2;
break;
}
}
}