//# include
using namespace std;
class student
{
public:
int num;
char name[20];
float score;
};
int main()
{
student std[5] = { 1001, "Li", 85, 1002, "Fun", 97.5, 1004, "Wang", 54, 1006, "Tan", 76.5, 1010, "Ling", 96 };
fstream iofile;
iofile.open("stud.dat", ios::in|ios::out|ios::binary);
if (!iofile)
{
cerr << "Error!" << endl;
exit(1);
}
for (int i = 0; i < 5; i++)
{
iofile.write((char *)&std[i], sizeof(std[i]));
}
student stud[5];
for (int i = 0; i < 5; i = i + 2)
{
iofile.seekg(i * sizeof(std[i]), 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_s(stud[2].name, 20, "Wu");
stud[2].score = 60;
iofile.seekp(2 * sizeof(stud[0]), ios::beg);
iofile.write((char *)&std[2], sizeof(std[2]));
iofile.seekg(0, ios::beg);
for (int i = 0; i < 5; i++)
{
iofile.read((char *)&stud[i], sizeof(stud[i]));
cout << stud[i].num << ' ' << stud[i].name << ' ' << stud[i].score << endl;
}
iofile.close();
return 0;
}
輸出的是Error,求解??看不出什麼錯誤!
http://zhidao.baidu.com/link?url=lTdfF6UxnroZlPazozUUph_PP0tuGkpX7SY2fkx0p4Up8VrSGs8a6JpINnzYxtammHiYzJBMvXrCpZfVe4QzZSipJvmRsolSIFUrHS__nLu