1 //**************************************************************************************************************
  2 //作者:
  3 //日期:2018/3/19
  4 //学号:
  5 //题号:5-5
  6 //题目:编写一个程序,实现以下功能:
  7
  8 //      1)输入一系列的学生数据(包括学生的学号、姓名和成绩等基本信息),将学生信息写入二进制文件student.dat中。
  9
 10 //      2)从student.dat文件中读出这些数据并显示出来。
 11
 12 //      3)在student.dat文件中按姓名进行查询,如输入“李”,则将所有姓“李”的学生的   数据显示出来。
 13
 14 //      4)可对指定学生的数据进行修改。
 15
 16 //      5)可以删除指定的学生数据。
 17
 18 //**************************************************************************************************************
 19
 20
 21
 22 #include<iostream>
 23 #include<fstream>
 24 #include<stdlib.h>
 25 #include<cstring>
 26 using namespace std;
 27 struct Std
 28 {
 29     char name[20];
 30     char id[20];
 31     char score[5];
 32 };
 33 void getinfo(char filename[]);
 34 void showinfo(char filename[]);
 35 void searchinfo(char filename[]);
 36 void editinfo(char filename[]);
 37 void deleteinfo(char filename[]);
 38
 39 int main()
 40 {
 41     fstream student;
 42     char choice;
 43     char filename[20];
 44     cout << "Please type in the filename:";
 45     cin.getline(filename,20);
 46     cout << endl;
 47     while(true)
 48     {
 49         cout << "Please type down the manipulation code:\nA.Input information\tB.Show information\tC.Search\tD.Edit\tE.Delete\tF.Exit\n";
 50         cout << endl;
 51         cin >> choice;
 52         switch(choice)
 53         {
 54             case'A':
 55             case'a':getinfo(filename);break;
 56             case'B':
 57             case'b':showinfo(filename);break;
 58             case'C':
 59             case'c':searchinfo(filename);break;
 60             case'D':
 61             case'd':editinfo(filename);break;
 62             case'E':
 63             case'e':deleteinfo(filename);break;
 64             case'F':
 65             case'f':exit(0);
 66         }
 67         cout << endl;
 68     }
 69     return 0;
 70 }
 71
 72 void getinfo(char filename[])
 73 {
 74     fstream student;
 75     student.open(filename,ios::out|ios::binary);
 76     if(!student)
 77     {
 78         cout << "Error!";
 79         exit(0);
 80     }
 81     int count;
 82     cout << "How many student's information would you like to input?";
 83     cin >> count;
 84     Std *Student;
 85     Student = new struct Std[count];
 86     for(int i=0;i<count;i++)
 87     {
 88         cout << "Please input student "<<i+1<<" 's name:";
 89         cin.get();
 90         cin.getline(Student[i].name,20);
 91         cout << "Please input student "<<i+1<<" 's id:";
 92         cin.getline(Student[i].id,20);
 93         cout << "Please input student "<<i+1<<" 's score:";
 94         cin >> Student[i].score;
 95         student.write((char*)&Student[i],sizeof(Student[i]));
 96     }
 97     cout << "Done.";
 98     delete[]Student;
 99     student.close();
100 }
101
102 void showinfo(char filename[])
103 {
104     fstream student;
105     student.open(filename,ios::in|ios::binary);
106     if(!student)
107     {
108         cout << "Error!";
109         exit(0);
110     }
111     Std Student;
112     student.read((char*)&Student,sizeof(Student));
113     while(!student.eof())
114     {
115         cout << "name:";
116         cout << Student.name <<endl;
117         cout << "id:";
118         cout << Student.id <<endl;
119         cout << "score:";
120         cout << Student.score <<endl;
121         student.read((char*)&Student,sizeof(Student));
122     }
123     cout << "Above are all the information."<<endl;
124     student.close();
125 }
126
127 void searchinfo(char filename[])
128 {
129     fstream student;
130     Std Student;
131     student.open(filename,ios::in|ios::binary);
132     if(!student)
133     {
134         cout << "Error!";
135         exit(0);
136     }
137     char ch,Name;
138     cout << "Please input the first name:";
139     cin >> Name;
140     long position = student.tellg();
141     cout << "position:"<<position<<endl;
142     student.seekg(0L,ios::beg);
143     student.get(ch);
144     cout << "ch:"<<ch<<endl;
145     //cout << "\nch:"<<ch<<endl;
146     position = student.tellg();
147     cout << "position:"<<position<<endl;
148     student.seekg(0L,ios::beg);
149     long current_pos = 0L;
150     while(!student.eof())
151     {
152         if(ch == Name)
153         {
154             cout << "ch:"<<ch<<endl;
155             student.read((char*)&Student,sizeof(Student));
156             long position;
157             position = student.tellg();
158             cout << "position:"<<position<<endl;
159             cout << "name:";
160             cout << Student.name <<endl;
161             cout << "id:";
162             cout << Student.id <<endl;
163             cout << "score:";
164             cout << Student.score <<endl;
165             student.seekg(-(sizeof(Student)),ios::cur);
166             position = student.tellg();
167             cout << "position:"<<position<<endl;
168         }
169         current_pos = current_pos + sizeof(Student);
170         student.seekg(current_pos,ios::cur);
171   //      long position = student.tellg();
172  //           cout << "position:"<<position<<endl;
173         //student.seekg(1L,ios::cur);
174         //cin.ignore();
175         //position = student.tellg();
176         //cout << "position:"<<position<<endl;
177  //       student.get(ch);
178  //       position = student.tellg();
179  //           cout << "position:"<<position<<endl;
180   //      student.seekg(-sizeof(ch),ios::cur);
181    //     position = student.tellg();
182 //s            cout << "position:"<<position<<endl;
183     }
184     cout << "Above are all the information that you are looking for."<<endl;
185     student.close();
186 }
187
188 void editinfo(char filename[])
189 {
190     fstream student;
191     Std Student;
192     student.open(filename,ios::in|ios::out|ios::binary);
193     if(!student)
194     {
195         cout << "Error!";
196         exit(0);
197     }
198     char editid[20];
199     cout << "Please input the id of the student whose information needs to be edited:";
200     cin >> editid;
201     student.seekg(0L,ios::beg);
202     student.seekg(sizeof(Student.name),ios::beg);
203     student.seekp(0L,ios::beg);
204     student.read((char*)&Student.id,sizeof(Student.id));
205     while(!student.eof())
206     {
207         if(strcmp(editid,Student.id)==0)
208         {
209             student.seekg(-sizeof(Student.id)-sizeof(Student.name),ios::cur);
210             student.read((char*)&Student,sizeof(Student));
211             cout << "The information of the student:"<<endl;
212             cout << "name:"<<Student.name<<endl;
213             cout << "id:"<<Student.id<<endl;
214             cout << "score:"<<Student.score<<endl;
215             cout << "Now please input the new information:";
216             cout << "name:";
217             cin >> Student.name;
218             cout << "id:";
219             cin >> Student.id;
220             cout << "score:";
221             cin >> Student.score;
222             student.write((char*)&Student,sizeof(Student));
223             break;
224         }
225         student.seekg(sizeof(Student),ios::cur);
226         student.seekp(sizeof(Student),ios::cur);
227     }
228     cout << "Done.";
229     student.close();
230 }
231
232 void deleteinfo(char filename[])
233 {
234     fstream student;
235     Std Student;
236     student.open(filename,ios::in|ios::out|ios::binary);
237     if(!student)
238     {
239         cout << "Error!";
240         exit(0);
241     }
242     char deleid[20];
243     cout << "Please input the id of the student whose information needs to be erased:";
244     cin >> deleid;
245     student.seekg(0L,ios::beg);
246     student.seekg(sizeof(Student.name),ios::beg);
247     student.seekp(0L,ios::beg);
248     student.read((char*)&Student.id,sizeof(Student.id));
249     while(!student.eof())
250     {
251         if(strcmp(deleid,Student.id)==0)
252         {
253             student.seekg(-sizeof(Student.id)-sizeof(Student.name),ios::cur);
254             student.read((char*)&Student,sizeof(Student));
255             cout << "The information of the student:"<<endl;
256             cout << "name:"<<Student.name<<endl;
257             cout << "id:"<<Student.id<<endl;
258             cout << "score:"<<Student.score<<endl;
259             cout << "type 'd' to delete this student's information:";
260             char code;
261             cin >> code;
262             if(code == 'd')
263             {
264                 strcpy(Student.name,0);
265                 strcpy(Student.id,0);
266                 strcpy(Student.score,0);
267             }
268             student.write((char*)&Student,sizeof(Student));
269             break;
270         }
271         student.seekg(sizeof(Student),ios::cur);
272         student.seekp(sizeof(Student),ios::cur);
273     }
274     cout << "Done.";
275     student.close();
276 }

转载于:https://www.cnblogs.com/Evanscabin/p/8652931.html

【生生被气死的一周】头秃相关推荐

  1. lisp语言与python_又要头秃?2020 年七大 AI 编程语言大盘点

    原标题:又要头秃?2020 年七大 AI 编程语言大盘点 本文讲述用于人工智能编程的顶级编程语言,AI工程师和相关从业者可从中选择适合他们项目所需要的语言,你Pick哪个呢? 作者 | Claire ...

  2. Go语言头秃之路(零)

    更新系列 Go语言头秃之路(一) Go语言头秃之路(二) Go语言头秃之路(三) Go语言头秃之路(四) Go语言头秃之路(五) Go语言头秃之路(六) Go语言头秃之路(七) 先导 指针 指针变量只 ...

  3. Go语言头秃之路(一)

    更新系列 Go语言头秃之路(零) Go语言头秃之路(二) Go语言头秃之路(三) Go语言头秃之路(四) Go语言头秃之路(五) Go语言头秃之路(六) Go语言头秃之路(七) 各种基础语句 cons ...

  4. Go语言头秃之路(五)

    更新系列 Go语言头秃之路(零) Go语言头秃之路(一) Go语言头秃之路(二) Go语言头秃之路(三) Go语言头秃之路(四) Go语言头秃之路(六) Go语言头秃之路(七) 文件操作 输入流:文件 ...

  5. Go语言头秃之路(七)

    更新系列 Go语言头秃之路(零) Go语言头秃之路(一) Go语言头秃之路(二) Go语言头秃之路(三) Go语言头秃之路(四) Go语言头秃之路(五) Go语言头秃之路(六) 反射 反射可以在运行时 ...

  6. Go语言头秃之路(三)

    更新系列 Go语言头秃之路(零) Go语言头秃之路(一) Go语言头秃之路(二) Go语言头秃之路(四) Go语言头秃之路(五) Go语言头秃之路(六) Go语言头秃之路(七) 数组 定义语法:var ...

  7. SSH整合过程中遇到的头秃问题

    若说遇到最头秃的问题是什么,那必然是404 若说遇到最要命的问题是什么,那必然是后台不报错的404 我真的会谢- 好啦,开始啦 问题一 如果struts配置文件中的extends="stru ...

  8. 写JS代码让自己头秃的点

    写JS代码让自己头秃的点 主要还是自己没系统学习js的锅吧,记录一下. 匿名函数内用this 匿名函数中,this指代的是window对象,不是直观理解中的,局部this,这就会产生undefine的 ...

  9. C/C++编程学习 - 第1周 ⑦ 头文件、强制类型转换、递归

    文章目录 头文件 引用头文件的语法 引用头文件的操作 只引用一次头文件 有条件引用 强制类型转换 整数提升 常用的算术转换 递归 数的阶乘 斐波那契数列 本周其他文章: 头文件 头文件包含了 C 函数 ...

最新文章

  1. java map prefix_从键以特定表达式开头的Map中获取所有值的最快方法
  2. 销量过亿的《剑指0ffer》题目和答案电子书 (附下载)
  3. 移动项目开发笔记(.Net下的观察者(Observer)模式)
  4. 腾讯后台开发面试题--整理1
  5. Vue.js 动态为img的src赋值
  6. 使用UAA OAuth2授权服务器–客户端和资源
  7. 内部类详解————匿名内部类
  8. php openssl des ecb,php7.2 des-ede3-ecb加密报错:openssl_encrypt():Unknown cipher algorithm 落叶随风博客...
  9. matlab m语言电路仿真,基于Matlab的TFT-LCD解码电路的仿真设计(含程序)
  10. hdu1426 Sudoku Killer
  11. 【免费模版分享】任务管理移动端Axure原型模板
  12. mirbase数据库简介
  13. Git问题解决方案:不建议在没有为偏离分支指定合并策略时执行pull操作(Pulling without specifying how to reconcile divergent branches)
  14. “零基础圆梦华为RS HCNP”视频课程规划
  15. 运算放大器的简要理解
  16. 拼多多分类ID搜索商品数据分析接口(商品列表数据,商品销量数据,商品详情数据)代码对接教程
  17. 随笔记录2、Android端调用系统分享文件记录
  18. Flashnbsp;AS3nbsp;学习9nbsp;-nbsp;时钟的制作
  19. 【UE4 第一人称射击游戏】08-使用“AK47”发射子弹
  20. codeforces900D Unusual Sequences

热门文章

  1. 《幸福资本论》读书笔记
  2. Win10如何查看我们的电池健康
  3. centos6.7 安装redis
  4. android连接sqlite进行简单的增删改查和事务管理
  5. Facebook大牛、HipHop作者赵海平加入阿里巴巴
  6. Docker Remote API v1.12使用
  7. Oracle 导出表结构
  8. Spring.NET学习笔记9——打造简易的依赖注入框架(练习篇) Level 100
  9. 对异步脉冲信号的处理——不归0翻转电路
  10. 虚拟机下Linux安装图解之三:Linux发行版本之Red Hat 9 安装