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 #include23 #include 24 #include 25 #include 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 > 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 < > Name;140 long position = student.tellg();141 cout << "position:"< < > 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:"< > 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:"< > 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 }