程序怎么运行结果不对啊?求指教
这是谭浩强c++课本上的程序怎么运行结果不对啊?求指教#include <iostream>
#include <fstream>
using namespace std;
struct student
{
int num;
char name[20];
float score;
};
int main()
{
student stud[5]={1001,"li",85,1002,"fun",97.5,1004,"wang",54,1006,"tan",76.5,1010,"ling",96};
ofstream outfile("stud.dat",ios::out|ios::binary);
if(!outfile)
{
cerr<<"open error!"<<endl;
abort();
}
ifstream infile("stud.dat",ios::in|ios::binary);
if(!infile)
{
cerr<<"open error!"<<endl;
abort();
}
for(int i=0;i<5;i++)
outfile.write((char *)&stud[i],sizeof(stud[i]));
student stud1[5];
for(int i=0;i<5;i=i+2)
{
infile.seekg(i*sizeof(stud[i]),ios::beg);
infile.read((char *)&stud1[i/2],sizeof(stud1[0]));
cout<<stud1[i/2].num<<" "<<stud1[i/2].name<<" "<<stud1[i/2].score<<endl;
}
cout<<endl;
stud[2].num=1012;
strcpy(stud[2].name,"wu");
stud[2].score=60;
outfile.seekp(2*sizeof(stud[0]),ios::beg);
outfile.write((char *)&stud[2],sizeof(stud[2]));
infile.seekg(0,ios::beg);
for(int i=0;i<5;i++)
{
infile.read((char *)&stud[i],sizeof(stud[i]));
cout<<stud[i].num<<" "<<stud[i].name<<" "<<stud[i].score<<endl;
}
infile.close();
outfile.close();
return 0;
}