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 while(!student.eof()) //最后一个数据块显示两次???141 {142 student.read((char*)&Student,sizeof(Student));143 ch = Student.name[0];144 if(ch == Name)145 {146 cout << "name:";147 cout << Student.name < > editid;173 student.seekp(0L,ios::beg);174 long current_position = 0;175 while(!student.eof())176 {177 student.read((char*)&Student,sizeof(Student)); //特别特别特别重要的三行!!!所以说又读又写是非常危险的!!!178 current_position += student.tellg();179 student.seekg(-sizeof(Student)+current_position,ios::beg);180 /*long position1 = student.tellp();181 cout << position1< > Student.name;194 cout << "id:";195 cin >> Student.id;196 cout << "score:";197 cin >> Student.score;198 /*long position1 = student.tellp();199 cout << position1< > deleid;224 long current_position = 0;225 while(!student.eof())226 {227 student.read((char*)&Student,sizeof(Student));228 strcpy(ID,Student.id);229 current_position = student.tellg();230 student.seekg(-sizeof(Student)+current_position,ios::beg);231 if(strcmp(deleid,ID)==0)232 {233 cout << "The information of the student:"< > code;240 if(code == 'd')241 {242 while(!student.eof())243 {244 current_position = student.tellg();245 student.seekg(-sizeof(Student)+current_position,ios::beg);246 }247 }248 249 student.write((char*)&Student,sizeof(Student));250 break;251 }252 }253 cout << "Done.";254 student.close();255 }
还是有点小问题,比如当检索的数据块为最后时会读取两次??删除也有问题。