1. 程式人生 > >C++ 文件操作(簡易的學籍管理系統)

C++ 文件操作(簡易的學籍管理系統)

學籍 () nbsp 系統 字符串 end bsp temp out

這是一個簡易的學籍管理系統,大一時居然三個人寫了一千多行......年少無知啊!歡迎摘果實!

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 }

C++ 文件操作(簡易的學籍管理系統)