C++谭浩强第四版13.5,二进制文件的操作

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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值