11_34.cpp

来自「c++案例教程源代码」· C++ 代码 · 共 31 行

CPP
31
字号
#include <iostream>
#include <fstream>
using namespace std;
class CStudent
{	struct student 
     {  char name[20];	
        char gender;		
        int age;		
        float avescore;
	}srec;
	fstream file;
public:
	CStudent();
	~CStudent();
	void printfile();
};
CStudent::CStudent()
{	file.open("student.bin",ios::in|ios::binary);
	if(file.fail())	{	cout<<"can not create file"<<endl;		return;	}
}
CStudent::~CStudent(){	file.close();}
void CStudent::printfile()
{	file.read((char*)&srec,sizeof(srec));
	while(!file.eof())
	{	cout<<"Name:"<<srec.name<<"," <<"Gender :"<<srec.gender<<",";
		cout<<"Age :"<<srec.age<<"," <<"Average Score:"<<srec.avescore<<endl;
		file.read((char*)&srec,sizeof(srec));
	}
}
void main(){	CStudent student;	student.printfile();}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?