c++ 基于文件的增删改查

╰半夏微凉° 2022-06-05 09:19 338阅读 0赞

wuzhixiang

  1. #include <iostream>
  2. #include <fstream>
  3. #include <sstream>
  4. #include <string>
  5. #include <iomanip>
  6. #include <windows.h>
  7. #define WIDTH 20
  8. #define space setw(WIDTH)
  9. #define NEWLINE "\r\n"
  10. using namespace std;
  11. void write(const char *filename)
  12. {
  13. ofstream out;
  14. out.open(filename,ios::app);
  15. if(!out.is_open())
  16. {
  17. cout<<"open error"<<endl;
  18. return ;
  19. }
  20. else
  21. {
  22. int id,age;
  23. string name;
  24. cout<<"请输入id、姓名、年龄:"<<endl;
  25. cin>>id>>name>>age;
  26. out.fill(' ');
  27. out.setf(ios::left);
  28. out<<space<<id<<space<<name<<space<<age<<NEWLINE;
  29. out.close();
  30. cout<<"添加成功!!"<<endl;
  31. }
  32. }
  33. void read(const char *filename)
  34. {
  35. ifstream in;
  36. in.open(filename,ios::in);
  37. if(!in.is_open())
  38. {
  39. cout<<"file open error"<<endl;
  40. return ;
  41. }
  42. else
  43. {
  44. stringstream ss;
  45. char line[100];
  46. cout.setf(ios::left);
  47. cout.fill(' ');
  48. cout<<space<<"id"<<space<<"name"<<space<<"age"<<endl;
  49. while(in.peek()!=EOF)
  50. {
  51. in.getline(line,100);
  52. ss<<line;
  53. int id,age;
  54. string name;
  55. ss>>id>>name>>age;
  56. cout<<space<<id<<space<<name<<space<<age<<endl;
  57. ss.str("");
  58. ss.clear();
  59. }
  60. in.close();
  61. }
  62. }
  63. void del(const char *filename)
  64. {
  65. int id;
  66. cout<<"请输入您要删除学生的编号"<<endl;
  67. cin>>id;
  68. ifstream in;
  69. in.open(filename,ios::in);
  70. if(!in.is_open())
  71. {
  72. cout<<"file open error"<<endl;
  73. return ;
  74. }
  75. else
  76. {
  77. string temp;
  78. stringstream ss;
  79. int curId;;
  80. while(in.peek()!=EOF)
  81. {
  82. string line;
  83. getline(in,line);
  84. ss<<line;
  85. ss>>curId;
  86. if(curId!=id)
  87. {
  88. temp += line + NEWLINE;
  89. }
  90. ss.str("");
  91. ss.clear();
  92. }
  93. in.close();
  94. ofstream out;
  95. out.open("G:\\students.txt",ios::out);
  96. if(!out.is_open())
  97. {
  98. cout<<"file open error"<<endl;
  99. return ;
  100. }
  101. else
  102. {
  103. out<<temp;
  104. out.close();
  105. cout<<"删除成功!!"<<endl;
  106. }
  107. }
  108. }
  109. int search_pos(const char *filename,int id)
  110. {
  111. ifstream in;
  112. in.open(filename,ios::in|ios::binary);
  113. if(!in.is_open())
  114. {
  115. cout<<"file open error"<<endl;
  116. return -1;
  117. }
  118. else
  119. {
  120. stringstream ss;
  121. while(in.peek()!=EOF)
  122. {
  123. int start = in.tellg();
  124. string line;
  125. getline(in,line);
  126. ss<<line;
  127. int curID;
  128. ss>>curID;
  129. if(curID == id)
  130. {
  131. in.close();
  132. return start;
  133. }
  134. ss.str("");
  135. }
  136. cout<<"对不起您查找的同学信息不存在!"<<endl;
  137. in.close();
  138. }
  139. return -1;
  140. }
  141. void search(const char *filename)
  142. {
  143. cout<<"请输入您要查找的学生id:"<<endl;
  144. int id;
  145. cin>>id;
  146. int pos = search_pos(filename,id);
  147. string line;
  148. fstream in;
  149. in.open(filename,ios::in|ios::binary);
  150. in.seekg(pos,ios::beg);
  151. getline(in,line);
  152. cout.setf(ios::left);
  153. cout<<space<<"id"<<space<<"name"<<space<<"age"<<endl;
  154. cout<<line<<endl;
  155. }
  156. void edit(const char *filename)
  157. {
  158. int id,age;
  159. string name;
  160. cout<<"请输入您要修改的学生id"<<endl;
  161. cin>>id;
  162. cout<<"请输入该学生新的姓名、年龄"<<endl;
  163. cin>>name>>age;
  164. stringstream infoTemp;
  165. infoTemp.fill(' ');
  166. infoTemp.setf(ios::left);
  167. infoTemp<<space<<id<<space<<name<<space<<age;
  168. string newInfo;
  169. getline(infoTemp,newInfo);
  170. fstream file;
  171. file.open(filename,ios::in|ios::out|ios::binary);
  172. if(!file.is_open())
  173. {
  174. cout<<"file open error"<<endl;
  175. return ;
  176. }
  177. else
  178. {
  179. int pos = search_pos(filename,id);
  180. file.seekg(pos,ios::beg);
  181. file<<newInfo;
  182. cout<<"修改后信息为:"<<endl;
  183. cout<<newInfo<<endl;
  184. file.close();
  185. }
  186. }
  187. int main()
  188. {
  189. const char *filename = "G:\\students.txt";
  190. while(true)
  191. {
  192. cout<<"--------------------------"<<endl;
  193. cout<<"0、查看全部信息"<<endl;
  194. cout<<"1、新增学生信息"<<endl;
  195. cout<<"2、删除学生信息"<<endl;
  196. cout<<"3、修改学生信息"<<endl;
  197. cout<<"4、查找学生信息"<<endl;
  198. int cmd;
  199. cin>>cmd;
  200. system("cls");
  201. switch(cmd)
  202. {
  203. case 0:read(filename);break;
  204. case 1:write(filename);break;
  205. case 2:del(filename);break;
  206. case 3:edit(filename);break;
  207. case 4:search(filename);break;
  208. }
  209. }
  210. return 0;
  211. }

发表评论

表情:
评论列表 (有 0 条评论,338人围观)

还没有评论,来说两句吧...

相关阅读