using namespace std;
struct staff {
int num;
char name[20];
int age;
double salary;
};
int main() {
staff st[7] = { 101,"lily",20,1239.1,102,"lisa",31,300,103,"iu",34,4500,1011,"ljy",21,100000,1012,"mrx",22,900000 }, stu;
fstream iofile("staff.dat", ios::in | ios::out | ios::binary);
cout << "show five staff:" << endl;
for (int i = 0; i < 5; i++) {
cout << st[i].num << " " << st[i].name << " " << st[i].age << " " << st[i].salary << endl;
iofile.write((char*)&st[i], sizeof(st[i]));
}
cout << "please enter you want insert:" << endl;
for (int i = 0; i < 2; i++) {
cin >> stu.num >> stu.name >> stu.age >> stu.salary;
iofile.write((char*)&stu, sizeof(stu));
}
cout << "show seven staff:" << endl;
iofile.seekg(0, ios::beg);
for (int i = 0; i < 7; i++) {
iofile.read((char*)&st[i], sizeof(st[i]));
cout << st[i].num << " " << st[i].name << " " << st[i].age << " " << st[i].salary << endl;
}
bool find;
cout << "enter you want find number:";
int num,m;
cin >> num;
while (num != 0) {
find = false;
for (int i = 0; i < 7; i++) {
if (num == st[i].num) {
find = true;
m = i+1;
}
}
if (find ==true) {
cout << "this is" << m << "th staff!!" << endl;
cout << st[m].num << " " << st[m].name << " " << st[m].age << " " << st[m].salary << endl;
}
else
cout << "nobody!" << endl;
cin >> num;
}
return 0;
}
C++谭浩强第四版13.5,二进制文件的操作
最新推荐文章于 2024-09-27 11:26:03 发布