📄 text1.cpp
字号:
#include<iostream.h>
#include<iomanip.h>
#include<fstream.h>
#include<string.h>
class CStudent
{
private:
char strName[10];
char strID[10];
float fScore;
public:
CStudent(char* name,char* id,float score=0);
void print();
friend ostream& operator<<(ostream& os,CStudent& stu);
friend istream& operator>>(istream& is,CStudent& stu);
};
CStudent::CStudent(char* name,char* id,float score)
{
strncpy(strName,name,10);
strncpy(strID,id,10);
fScore=score;
}
void CStudent::print()
{
cout<<endl<<"学生信息如下:"<<endl;
cout<<"姓名:"<<strName<<endl;
cout<<"学号:"<<strID<<endl;
cout<<"成绩:"<<fScore<<endl;
}
ostream& operator<<(ostream& os,CStudent& stu)
{
os.write(stu.strName,10);
os.write(stu.strID,10);
os.write((char*)&stu.fScore,4);
return os;
}
istream& operator>>(istream& is,CStudent& stu)
{
char name[10],id[10];
is.read(name,10);
is.read(id,10);
is.read((char*)&stu.fScore,4);
strncpy(stu.strName,name,10);
strncpy(stu.strID,id,10);
return is;
}
void main()
{
CStudent stu1("mawentao","99001",88);
CStudent stu2("liming","99002",93);
CStudent stu3("wangfang","99003",89);
CStudent stu4("yangyang","99004",90);
CStudent stu5("dingning","99005",80);
fstream file1;
file1.open("student.doc",ios::out|ios::in|ios::binary);
file1<<stu1<<stu2<<stu3<<stu4<<stu5;
CStudent* one=new CStudent("","");
const int size=24;
file1.seekp(size*4);file1>>*one;one->print();
file1.seekp(size*1);file1>>*one;one->print();
file1.seekp(size*2,ios::cur);file1>>*one;one->print();
file1.close();
delete one;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -