C++ 文件操作(简易的学籍管理系统)

这是一个简易的学籍管理系统,大一时居然三个人写了一千多行......年少无知啊!欢迎摘果实!

1
#include <iostream> 2 #include <fstream> 3 #include <string> 4 #include <sstream> 5 using namespace std; 6 7 /* 8 *信息结构 9 */ 10 /*struct stu 11 { 12 string id; 13 string name; 14 string gender; 15 int age; 16 string magor; 17 string prize; 18 };*/ 19 20 bool isFind = false;//是否找到的标识 21 22 void find(const char *file, const int id); 23 void del(const char *file, const int id); 24 void change(const char *file, const int id); 25 26 void add(const char *file) 27 { 28 ofstream out; 29 out.open(file, ios::app); 30 if (!out.is_open()) 31 { 32 cout << "文件打开失败" << endl; 33 return; 34 } 35 else 36 { 37 int id, age; 38 string name, major, prize, gender; 39 cout << "输入学号、姓名、性别、年龄、专业、奖项:" << endl; 40 cin >> id >> name >> gender >> age >> major >> prize; 41 find(file, id); 42 if(!isFind) 43 out << id << "\n" << name << "\n" << gender << "\n" << age << "\n" << major << "\n" << prize << "\n"; 44 else 45 { 46 cout << "该学号已存在..." << endl; 47 return; 48 } 49 cout << "添加成功" << endl; 50 out.close(); 51 } 52 } 53 54 void find(const char *file, const int id) 55 { 56 ifstream in; 57 in.open(file, ios::in | ios::binary); 58 if (!in.is_open()) 59 { 60 cout << "打开文件错误" << endl; 61 return ; 62 } 63 while(in.peek() != EOF) 64 { 65 int temp; 66 stringstream s; 67 string line; 68 getline(in, line);//读字符串 69 s << line; 70 s >> temp; 71 if (temp == id) 72 { 73 isFind = true;//找到 74 int age; 75 string name, major, prize, gender; 76 cout << "找到记录:"<< endl; 77 getline(in, line);//读名字 78 s << line; s >> name; 79 getline(in, line);//读性别 80 s << line; s >> gender; 81 getline(in, line);//读年龄 82 s << line; s >> age; 83 getline(in, line);//读专业 84 s << line; s >> major; 85 getline(in, line);//读奖项 86 s << line; s >> prize; 87 88 cout << "学号:" << temp << " "; 89 cout << "姓名:" << name << " "; 90 cout << "性别:" << gender << " "; 91 cout << "年龄:" << age << " "; 92 cout << "专业:" << major << endl; 93 cout << "奖项:" << prize << endl; 94 } 95 } 96 in.close(); 97 } 98 99 void del(const char *file, const int id) 100 { 101 isFind = false; 102 find(file, id);//找到要删的位置 103 if(isFind) 104 cout << "正在删除..." << endl; 105 else 106 { 107 cout << "无此纪录!" << endl; 108 isFind = false; 109 return; 110 } 111 ifstream in; 112 in.open(file, ios::in | ios::binary); 113 if (!in.is_open()) 114 { 115 cout << "打开文件错误" << endl; 116 return; 117 } 118 string tempStr; 119 while (in.peek() != EOF) 120 { 121 int temp; 122 string line; 123 getline(in, line);//读字符串 124 stringstream s; 125 s << line; 126 s >> temp; 127 if (temp == id) 128 { 129 int delLine = 5; 130 while (delLine--) 131 getline(in, line); 132 } 133 else 134 { 135 //getline(in, line);//读字符串 136 tempStr += line; 137 tempStr += "\n"; 138 } 139 140 } 141 in.close(); 142 //重新写入文件 143 ofstream out; 144 out.open(file, ios::out); 145 if (!out.is_open()) 146 { 147 cout << "打开文件错误,删除失败!" << endl; 148 return; 149 } 150 else 151 { 152 out << tempStr;//重新写入 153 cout << "删除完成!" << endl; 154 } 155 out.close(); 156 } 157 158 void change(const char *file, const int id) 159 { 160 isFind = false; 161 find(file, id);//找到要改的目标 162 if (isFind) 163 { 164 int age; 165 string name, major, prize, gender; 166 cout << "输入新的姓名、性别、年龄、专业、奖项:" << endl; 167 cin >> name >> gender >> age >> major >> prize; 168 ifstream in; 169 in.open(file, ios::in | ios::binary); 170 if (!in.is_open()) 171 { 172 cout << "文件打开失败!" << endl; 173 return; 174 } 175 string tempStr; 176 while (in.peek() != EOF) 177 { 178 int temp; 179 string line; 180 stringstream s; 181 getline(in, line); 182 s << line; 183 s >> temp; 184 if (temp == id) 185 { 186 tempStr += to_string(id) + "\n"; 187 tempStr += name + "\n"; 188 tempStr += gender + "\n"; 189 tempStr += to_string(age) + "\n"; 190 tempStr += major + "\n"; 191 tempStr += prize + "\n";//加入新信息 192 int delLine = 5; 193 while (delLine--) 194 getline(in, line);//跳过旧信息 195 } 196 else 197 { 198 tempStr += line; 199 tempStr += "\n"; 200 } 201 } 202 in.close();//别忘记 203 204 //重新写入文件 205 ofstream out; 206 out.open(file, ios::out); 207 if (!out.is_open()) 208 { 209 cout << "打开文件错误,删除失败!" << endl; 210 return; 211 } 212 else 213 { 214 out << tempStr;//重新写入 215 cout << "修改完成!" << endl; 216 } 217 out.close(); 218 } 219 } 220 221 int main() 222 { 223 const char *file = "D:\\homework\\DB\\test.txt";//文件地址 224 while (true) 225 { 226 int ans; 227 cout << "--------------------------------" << endl; 228 cout << "--> 1.插入信息 <--" << endl; 229 cout << "--> 2.查找信息 <--" << endl; 230 cout << "--> 3.删除信息 <--" << endl; 231 cout << "--> 4.修改信息 <--" << endl; 232 cout << "--> 0.退出Demo <--" << endl; 233 cout << "--------------------------------" << endl; 234 cout << "输入指令~$ "; 235 cin >> ans; 236 switch (ans) 237 { 238 case 0: 239 { 240 cout << "已退出!" << endl; 241 return 0; 242 } 243 break; 244 case 1: 245 { 246 add(file); 247 } 248 break; 249 case 2: 250 { 251 cout << "输入要查找的学号:"; 252 int id; 253 cin >> id; 254 find(file, id); 255 } 256 break; 257 case 3: 258 { 259 cout << "输入要删除的学生学号:"; 260 int id; 261 cin >> id; 262 del(file, id); 263 } 264 break; 265 case 4: 266 { 267 cout << "输入要修改的学生学号:"; 268 int id; 269 cin >> id; 270 change(file, id); 271 } 272 default: 273 { 274 cout << "输入有误!" << endl; 275 } 276 break; 277 } 278 } 279 280 return 0; 281 }

猜你喜欢

转载自www.cnblogs.com/yocichen/p/10453529.html