第七章第五题
2015-06-02 19:53
344 查看
#include <iostream> #include <fstream> using namespace std; struct staff {int num; char name[20]; int age; double pay; }; int main() {staff staf[7]={3898,"Fan",34,1200,3899,"Wang",23,6740,2448,"Chen",54,778, 3000,"Du",45,4763,3301,"Ling",39,6555},staf1; fstream iofile("staff.dat",ios::in|ios::out|ios::binary); if(!iofile) {cerr<<"open error!"<<endl; abort(); } int i,m,num; cout<<"Five staff :"<<endl; for(i=0;i<5;i++) {cout<<staf[i].num<<" "<<staf[i].name<<" "<<staf[i].age<<" "<<staf[i].pay<<endl; iofile.write((char *)&staf[i],sizeof(staf[i]));} cout<<"please input data you want insert:"<<endl; for(i=0;i<2;i++) {cin>>staf1.num>>staf1.name>>staf1.age>>staf1.pay; iofile.seekp(0,ios::end); iofile.write((char *)&staf1,sizeof(staf1));} iofile.seekg(0,ios::beg); for(i=0;i<7;i++) {iofile.read((char *)&staf[i],sizeof(staf[i])); cout<<staf[i].num<<" "<<staf[i].name<<" "<<staf[i].age<<" "<<staf[i].pay<<endl; } bool find; cout<<"enter number you want search,enter 0 to stop."; cin>>num; while(num) {find=false; iofile.seekg(0,ios::beg); for(i=0;i<7;i++) {iofile.read((char *)&staf[i],sizeof(staf[i])); if(num==staf[i].num) {m=iofile.tellg(); cout<<num<<" is No."<<m/sizeof(staf1)<<endl; cout<<staf[i].num<<" "<<staf[i].name<<" "<<staf[i].age<<" "<<staf[i].pay<<endl; find=true; break; } } if(!find) cout<<"can't find "<<num<<endl; cout<<"enter number you want search,enter 0 to stop."; cin>>num; } iofile.close(); return 0; }
相关文章推荐
- 【转】style、id、class和*的优先级
- iOS symbol化的多种方式
- Android使用第三方库.so被删掉问题
- Letex查询
- MTK6577+Android4.0背光点亮
- WCF广州本田整车销售系统技术解析(四) 订单修改功能实现分析
- OpenCV学习笔记06--数据存储、配置文件
- java中的TreeMap和HashMap的区别和联系:
- Java异常处理
- Activity之间的切换效果
- xshell 登录后自动跳转设置
- 网易、支付宝、携程宕机拷问数据安全问题
- 002_01CSS
- linux 编译ffmpeg 支持x264, x265
- Android手机的所需的一些权限
- 修改游戏图标
- 实现医生工作站的历次处方功能的代码(八)
- 数据库 三范式最简单最易记的解释
- 关于Layout_weight的理解
- Backlog和冲刺结果以及产品Demo市场调研