不懂就问,编译过了,为啥结果输不出呢
#include<iostream>
#include<iomanip>
#include<fstream>
#include<string>
#define _CRT_SECURE_NO_WARNINGS
using namespace std;
class CStudent
{
public:
CStudent(const char* name,const char* id, float score = 0);
void print();//输出
friend ostream& operator<<(ostream& os, CStudent& stu);
friend istream& operator>>(istream& is, CStudent& stu);
private:
char strName[10];
char strID[10];
float fScore;
};
CStudent::CStudent(const char* name,const char* id,float score)
{
strncpy_s(strName, name, strlen(strName)+1);
strncpy_s(strID, id,strlen(strID)+1);
fScore = score;
}
void CStudent::print()
{
cout << endl << "学生信息如下:" << endl;
cout << "姓名:" << strName << endl;
cout << "学号:" << strID << endl;
cout << "成绩:" << fScore << endl;
}
std::ostream& operator<<(ostream& os, CStudent& stu)
{
//使用定长格式,使每一个类的数据长度为24个字节
os.write(stu.strName, 10);
os.write(stu.strID, 10);
os.write((char*)& stu.fScore,4);//使float数据类型占4字节
return os;
}
std::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);//读入4字节数据作为float类型
strncpy_s(stu.strName, name, strlen(stu.strName)+1);
strncpy_s(stu.strID, id, strlen(stu.strID)+1);
return is;
}
void main()
{
CStudent stu1("MaWenTao", "99001", 88);
CStudent stu2("LiMing", "99002", 92);
CStudent stu3("WangFang", "99003", 89);
CStudent stu4("YangYang", "99004", 90);
CStudent stu5("DingNing", "99005", 80);
fstream file1;
file1.open("student.dat", ios::out | ios::in | ios::binary);
file1 << stu1 << stu2 << stu3 << stu4 << stu5;
CStudent* one = new CStudent("","");
const int size = 24;//每一个类的数据长度为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;
}