Advertisement
Anwar_Rizk

Untitled

Apr 19th, 2022 (edited)
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.20 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string.h>
  4.  
  5. using namespace std;
  6.  
  7. class student {
  8. public:
  9. int age, id;
  10. char name[10];
  11. };
  12.  
  13. void addStudent(){
  14. ofstream f("student.txt", ios::out | ios::app | ios::ate);
  15. student s1;
  16. char c;
  17. do {
  18. cout << "Enter ID: ";
  19. cin >> s1.id;
  20. cout << "Enter Name: ";
  21. cin >> s1.name;
  22. cout << "Enter Age: ";
  23. cin >> s1.age;
  24. f.write((char*) &s1, sizeof(s1));
  25. cout << "Do you want to enter again? (Y/N): ";
  26. cin >> c;
  27. } while(tolower(c) == 'y');
  28. f.close();
  29. }
  30.  
  31. void readAllStudents(){
  32. student s1;
  33. ifstream in;
  34. in.open("student.txt", ios::in);
  35. if(in.is_open())
  36. {
  37. cout << "ID\tName\tAge\n\n";
  38. in.read((char*)& s1, sizeof(s1));
  39. while(!in.eof()){
  40. cout << s1.id << "\t" << s1.name << "\t" << s1.age << "\n";
  41. in.read((char*)& s1, sizeof(s1));
  42. }
  43. in.close();
  44. }
  45. else cout << "Can't open the specified file...\n";
  46. }
  47.  
  48. void searchByID(){
  49. student s1;
  50. int ID;
  51. cout << "Enter an ID to search: ";
  52. cin >> ID;
  53. ifstream in;
  54. in.open("student.txt", ios::in);
  55. if(in.is_open())
  56. {
  57. bool exist = false;
  58. in.read((char*)& s1, sizeof(s1));
  59. while(!in.eof()){
  60. if(ID == s1.id){
  61. cout << "ID\tName\tAge\n\n";
  62. cout << s1.id << "\t" << s1.name << "\t" << s1.age << "\n";
  63. exist = true;
  64. break;
  65. }
  66. in.read((char*)& s1, sizeof(s1));
  67. }
  68. if(!exist) cout << "Not exist....\n";
  69. in.close();
  70. }
  71. else cout << "Can't open the specified file...\n";
  72. }
  73.  
  74. void searchByName(){
  75. student s1;
  76. char Name[10];
  77. cout << "Enter a name to search: ";
  78. cin >> Name;
  79. ifstream in;
  80. in.open("student.txt", ios::in);
  81. if(in.is_open())
  82. {
  83. bool exist = false;
  84. in.read((char*)& s1, sizeof(s1));
  85. while(!in.eof()){
  86. if(strcmp(Name, s1.name) == 0){
  87. cout << "ID\tName\tAge\n\n";
  88. cout << s1.id << "\t" << s1.name << "\t" << s1.age << "\n";
  89. exist = true;
  90. break;
  91. }
  92. in.read((char*)& s1, sizeof(s1));
  93. }
  94. if(!exist) cout << "Not exist....\n";
  95. in.close();
  96. }
  97. else cout << "Can't open the specified file...\n";
  98. }
  99.  
  100. void updateStudent(){
  101. student s1;
  102. char Name[10];
  103. cout << "Enter a name to edit his data: ";
  104. cin >> Name;
  105. fstream in;
  106. in.open("student.txt", ios::in | ios::out);
  107. if(in.is_open())
  108. {
  109. bool exist = false;
  110. in.read((char*)& s1, sizeof(s1));
  111. while(!in.eof())
  112. {
  113. if(strcmp(Name, s1.name) == 0)
  114. {
  115. int ch;
  116. char c;
  117. f:
  118. cout << "Choose what you need to edit :\n";
  119. cout << "1) ID\n2) Name\n3) Age\n\t:";
  120. cin >> ch;
  121. switch(ch)
  122. {
  123. case 1:
  124. cout << "Enter the new ID: ";
  125. cin >> s1.id;
  126. break;
  127. case 2:
  128. cout << "Enter the new Name: ";
  129. cin >> s1.name;
  130. break;
  131. case 3:
  132. cout << "Enter the new Age: ";
  133. cin >> s1.age;
  134. break;
  135. default:
  136. cout << "Error...\n";
  137. goto f;
  138. }
  139. cout << "Do you want to edit more? (Y|N): ";
  140. cin >> c;
  141. if(tolower(c) != 'n') goto f;
  142. int curpos = in.tellg();
  143. in.seekp(curpos - sizeof(s1), ios::beg);
  144. in.write((char*) &s1, sizeof(s1));
  145. in.seekg(curpos - sizeof(s1), ios::beg);
  146. in.read((char*) &s1, sizeof(s1));
  147. cout << "ID\tName\tAge\n\n";
  148. cout << s1.id << "\t" << s1.name << "\t" << s1.age << "\n";
  149. exist = true;
  150. break;
  151. }
  152. in.read((char*)& s1, sizeof(s1));
  153. }
  154. if(!exist) cout << "Not exist....\n";
  155. in.close();
  156. }
  157. else cout << "Can't open the specified file...\n";
  158. }
  159.  
  160. void deleteStudent(){
  161. ifstream in("student.txt", ios::in);
  162. ofstream out("temp.txt", ios::out);
  163. student s1;
  164. cout << "Enter a name to delete: ";
  165. char Name[10];
  166. cin >> Name;
  167. if(in.is_open())
  168. {
  169. bool exist = false;
  170.  
  171. in.read((char*)& s1, sizeof(s1));
  172. while(!in.eof()){
  173. if(strcmp(Name, s1.name) != 0){
  174. out.write((char*) &s1, sizeof(s1));
  175. }
  176. else exist = true;
  177. in.read((char*)& s1, sizeof(s1));
  178. }
  179. in.close();
  180. out.close();
  181. remove("student.txt");
  182. rename("temp.txt", "student.txt");
  183. if(!exist) cout << "Not exist....\n";
  184. }
  185. else cout << "Can't open the specified file...\n";
  186. }
  187.  
  188. int main()
  189. {
  190. cout << "\n";
  191. int opt;
  192. do {
  193. start:
  194. cout << "\nchoose an option :\n";
  195. cout << "1] add student\n2] print all students\n3] search\n4] edit student data\n5] delete student\n0] Exit\n";
  196. cin >> opt;
  197. switch(opt)
  198. {
  199. case 1:
  200. addStudent();
  201. break;
  202. case 2:
  203. readAllStudents();
  204. break;
  205. case 3:
  206. int ch;
  207. ff:
  208. cout << "by Name or ID :\n";
  209. cout << "1- by name\n2- by ID\nenter choice: ";
  210. cin >> ch;
  211. if(ch == 1) searchByName();
  212. else if(ch == 2) searchByID();
  213. else {
  214. cout << "wrong!!\n";
  215. goto ff;
  216. }
  217. break;
  218. case 4:
  219. updateStudent();
  220. break;
  221. case 5:
  222. deleteStudent();
  223. break;
  224. case 0:
  225. return 0;
  226. default:
  227. cout << "Wrong choice...\nchoose again\n";
  228. goto start;
  229. }
  230. }while(opt != 0);
  231.  
  232. return cout << "\n", 0;
  233. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement