【解决】
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 Name,ch; 138 cout << "Please input the first name:"; 139 cin >> 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 <<endl; 148 cout << "id:"; 149 cout << Student.id <<endl; 150 cout << "score:"; 151 cout << Student.score <<endl; 152 } 153 cout << endl; 154 } 155 cout << "Above are all the information that you are looking for."<<endl; 156 cin.ignore(); 157 student.close(); 158 } 159 160 void editinfo(char filename[]) 161 { 162 fstream student; 163 Std Student; 164 student.open(filename,ios::in|ios::out|ios::binary); 165 if(!student) 166 { 167 cout << "Error!"; 168 exit(0); 169 } 170 char editid[20],ID[20]; 171 cout << "Please input the id of the student whose information needs to be edited:"; 172 cin >> 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<<endl; 182 long position2 = student.tellg(); 183 cout << position2<<endl;*/ 184 strcpy(ID,Student.id); 185 if(strcmp(editid,ID)==0) 186 { 187 cout << "The information of the student:"<<endl; 188 cout << "name:"<<Student.name<<endl; 189 cout << "id:"<<Student.id<<endl; 190 cout << "score:"<<Student.score<<endl; 191 cout << "Now please input the new information:"<<endl; 192 cout << "name:"; 193 cin >> Student.name; 194 cout << "id:"; 195 cin >> Student.id; 196 cout << "score:"; 197 cin >> Student.score; 198 /*long position1 = student.tellp(); 199 cout << position1<<endl;*/ 200 student.write((char*)&Student,sizeof(Student)); 201 /*long position2 = student.tellp(); 202 cout << position2<<endl;*/ 203 break; 204 } 205 } 206 cout << endl; 207 cout << "Done."; 208 student.close(); 209 } 210 211 void deleteinfo(char filename[]) 212 { 213 fstream student; 214 Std Student; 215 student.open(filename,ios::in|ios::binary); 216 if(!student) 217 { 218 cout << "Error!"; 219 exit(0); 220 } 221 char deleid[20],ID[20]; 222 cout << "Please input the id of the student whose information needs to be erased:"; 223 cin >> 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:"<<endl; 234 cout << "name:"<<Student.name<<endl; 235 cout << "id:"<<Student.id<<endl; 236 cout << "score:"<<Student.score<<endl; 237 cout << "type 'd' to delete this student's information:"; 238 char code; 239 cin >> 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 }
还是有点小问题,比如当检索的数据块为最后时会读取两次??删除也有问题。
------------------
有为,有守,有趣。
------------------