#include<iostream>
#include<fstream>
using namespace std;
struct student
{
int num;
char name[30];
char score;
};
int main()
{
student stud[5] = { 1001,"li",85,1002,"Fang",97.5,1004,"Wang",56,1006,"Tian",76.5,1010,"ling",96 };
fstream iofile("stud.dat", ios::in | ios::out | ios::binary);
if (!iofile)
{
cerr << "open error!" << endl;
abort();
}
for (int i = 0; i < 5; i = i ++)
iofile.write((char*)& stud+, sizeof(stud+));
student stud[5];
for (int i = 0; i < 5; i = i + 2)
{
iofile.seekg(i * sizeof(stud+), ios::beg);
iofile.read((char*)& stud[i / 2], sizeof(stud[0]));
cout << stud[i / 2].num << " " << stud[i / 2].name << " " << stud[i / 2].score << endl;
cout << endl;
}
stud[2].num = 1012;
strcpy(stud[2].name, "wu");
stud[2].score = 60;
iofile.seekp(2 * sizeof(stud[0]), ios::beg);
iofile.write((char*)& stud[2], sizeof(stud[2]));
iofile.seekg(0, ios::beg);
for (int i = 0; i < 5; i++)
{
iofile.read((char*)& stud+, sizeof(stud+));
cout << stud+.num << " " << stud+.name << " " << stud+.score << endl;
}
iofile.close();
system("pause");
return 0;
}