fork download
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <stdio.h>
  3. #include <windows.h>
  4. #include <conio.h>
  5.  
  6. struct DATA
  7. {
  8. int number=0;
  9. char surname[12], name[10], patronymic[15];
  10. int BirthDate, BirthMonth, BirthYear;
  11. char addresSTREET[16];
  12. int addresHOME;
  13. int mobileNUMBER;
  14. };
  15.  
  16. int ENTER_CHOICE(void);
  17. void NEW_DATA(FILE*);
  18. void VIEW_DATA(FILE*);
  19. void ENTER_DATA(struct DATA*);
  20. void UPDATE_RECORD(FILE*);
  21. void ADD_RECORD(FILE*);
  22. void DELETE_RECORD(FILE*);
  23. void SEARCH_RECORD(FILE*);
  24. void SORT_DATA(FILE*);
  25. int CHECK_INTEGER(int,int);
  26.  
  27. int main()
  28. {
  29. //Русификация консоли ввода/вывода
  30. SetConsoleCP(1251); //установка кодовой страницы win-cp 1251 в поток ввода
  31. SetConsoleOutputCP(1251); //установка кодовой страницы win-cp 1251 в поток вывода
  32.  
  33. FILE* cfPtr;
  34. char fname[20];
  35. int choice;
  36. int access_mode;
  37.  
  38. printf("Продолжить работу в старом файле?\n"
  39. "(1)-Да (0)-Нет, я хочу создать новый\n->");
  40. scanf("%d", &access_mode);
  41.  
  42. printf("Введите название файла (name.txt)\n-> ");
  43. //while (getchar() != '\n');
  44. //fgets(fname, sizeof(fname), stdin);
  45. scanf("%[^\n]", fname);
  46.  
  47. do {
  48. system("cls");
  49.  
  50. if (access_mode == 1) {
  51. if ((cfPtr = fopen(fname, "r+")) == NULL) {
  52. printf("File could not be opened.\n");
  53. return 0;
  54. }
  55. }
  56. else {
  57. if ((cfPtr = fopen(fname, "w+")) == NULL) {
  58. printf("File could not be opened.\n");
  59. return 0;
  60. }
  61. access_mode = 1;
  62. }
  63.  
  64. choice = ENTER_CHOICE();
  65.  
  66. switch (choice)
  67. {
  68. case 0: return 0;
  69. case 1:
  70. NEW_DATA(cfPtr);
  71. break;
  72. case 2:
  73. VIEW_DATA(cfPtr);
  74. break;
  75. case 3:
  76. UPDATE_RECORD(cfPtr);
  77. break;
  78. case 4:
  79. ADD_RECORD(cfPtr);
  80. break;
  81. case 5:
  82. DELETE_RECORD(cfPtr);
  83. break;
  84. case 6:
  85. SEARCH_RECORD(cfPtr);
  86. break;
  87. case 7:
  88. SORT_DATA(cfPtr);
  89. break;
  90. }
  91. fclose(cfPtr);
  92. if (choice != 0)
  93. system("\npause");
  94. } while (choice!=0);
  95. }
  96.  
  97. int ENTER_CHOICE(void)
  98. {
  99. int choice;
  100. char buf[100]; // строка для считывания введённых данных
  101.  
  102. printf("Выберите, что вам нужно сделать\n"
  103. "0 -> Выход из программы\n"
  104. "1 -> Создание новой базы данных\n"
  105. "2 -> Просмотр существующей базы данных\n"
  106. "3 -> Редактирование базы данных\n"
  107. "4 -> Дополнение базы данных новыми записями\n"
  108. "5 -> Удаление записей из базы данных\n"
  109. "6 -> Поиск в базе данных\n"
  110. "7 -> Сортировка пользователей по фамилии\n-> ");
  111. /*scanf("%s", buf); */
  112. fgets(buf, sizeof(buf), stdin);// считываем строку
  113. // пока ввод некорректен, сообщаем об этом и просим повторить его
  114. while (sscanf(buf, "%d", &choice) != 1 || choice < 0 || choice > 7)
  115. {
  116. printf("Неверно введено значение. Попробуйте снова ->"); // выводим сообщение об ошибке
  117. /*scanf("%s", buf);*/
  118. fgets(buf, sizeof(buf), stdin);// считываем строку повторно
  119. }
  120. return choice;
  121. }
  122.  
  123. void NEW_DATA(FILE* fPtr)
  124. {
  125. int ans = 1;
  126. struct DATA notebook;
  127.  
  128. do{
  129. notebook.number++;
  130. ENTER_DATA(&notebook);
  131.  
  132. fseek(fPtr, notebook.number * sizeof(struct DATA), SEEK_SET);
  133. fwrite(&notebook, sizeof(struct DATA), 1, fPtr);
  134. printf("Введите (1) - чтобы добавить еще (0) - закончить\n-> ");
  135. scanf("%d", &ans);
  136. } while (ans != 0);
  137. }
  138.  
  139. void VIEW_DATA(FILE* readPtr)
  140. {
  141. DATA buf;
  142. printf("%-2s| %-12s | %-10s | %-15s | %-13s | %-16s | %-11s |\n",
  143. "№","Фамилия","Имя","Отчество","Дата Рождения","Адрес","моб.телефон");
  144. fseek(readPtr, 0 * sizeof(struct DATA), SEEK_SET);
  145. while (fread(&buf, sizeof(buf), 1, readPtr)) {
  146.  
  147. if(buf.number!=0)
  148. printf("%-2d| %-12s | %-10s | %-15s | %02d.%02d.%04d | %-12s,%-3d | %-11d |\n",
  149. buf.number, buf.surname, buf.name, buf.patronymic,
  150. buf.BirthDate, buf.BirthMonth, buf.BirthYear,
  151. buf.addresSTREET, buf.addresHOME, buf.mobileNUMBER);
  152. }
  153. }
  154.  
  155. int CHECK_INTEGER(int a, int b)
  156. {
  157. char buf[256];
  158. int user_number;
  159.  
  160. fgets(buf, sizeof(buf), stdin);
  161. while (sscanf(buf, "%d", &user_number) != 1 || user_number < a || user_number > b)
  162. {
  163. printf("Ошибка ввода. Попробуйте еще раз\n-> ");
  164. fgets(buf, sizeof(buf), stdin);
  165. }
  166. return user_number;
  167. }
  168.  
  169. void ENTER_DATA(struct DATA* notebook)
  170. {
  171. while (getchar() != '\n');
  172. printf("Введите фамилию -> ");
  173. fgets(notebook->surname, sizeof(notebook->surname), stdin);
  174. notebook->surname[strcspn(notebook->surname, "\n")] = 0;
  175.  
  176. printf("Введите имя -> ");
  177. fgets(notebook->name, sizeof(notebook->name), stdin);
  178. notebook->name[strcspn(notebook->name, "\n")] = 0;
  179.  
  180. printf("Введите отчество -> ");
  181. fgets(notebook->patronymic, sizeof(notebook->patronymic), stdin);
  182. notebook->patronymic[strcspn(notebook->patronymic, "\n")] = 0;
  183.  
  184. printf("Введите дату рождения:\nДень -> ");
  185. notebook->BirthDate=CHECK_INTEGER(1, 31);
  186. printf("Месяц -> ");
  187. notebook->BirthMonth = CHECK_INTEGER(1, 12);
  188. printf("Год -> ");
  189. notebook->BirthYear = CHECK_INTEGER(1950, 2021);
  190.  
  191. printf("Введите адрес:\nУлица -> ");
  192. fgets(notebook->addresSTREET, sizeof(notebook->addresSTREET), stdin);
  193. notebook->addresSTREET[strcspn(notebook->addresSTREET, "\n")] = 0;
  194.  
  195. printf("Номер дома -> ");
  196. notebook->addresHOME = CHECK_INTEGER(1, 999);
  197.  
  198. printf("Введите номер мобильного телефона -> ");
  199. notebook->mobileNUMBER = CHECK_INTEGER(1, 999999999);
  200. }
  201.  
  202. void UPDATE_RECORD(FILE* fPtr)
  203. {
  204. struct DATA notebook;
  205. int numper;
  206. VIEW_DATA(fPtr);
  207. printf("Введите номер человека, данные которого нужно изменить\n-> ");
  208. /*scanf_s("%d", &numper);*/
  209. while (getchar() != '\n');
  210. numper = CHECK_INTEGER(1, 999);
  211.  
  212. notebook.number = numper;
  213. ENTER_DATA(&notebook);
  214.  
  215. fseek(fPtr, (notebook.number) * sizeof(struct DATA), SEEK_SET);
  216. fwrite(&notebook, sizeof(struct DATA), 1, fPtr);
  217. }
  218.  
  219. void ADD_RECORD(FILE* fPtr)
  220. {
  221. struct DATA notebook;
  222. int ans = 1;
  223. int numper = 0;
  224. VIEW_DATA(fPtr);
  225. fseek(fPtr, 0 * sizeof(struct DATA), SEEK_SET);
  226. while (fread(&notebook, sizeof(notebook), 1, fPtr) == 1)
  227. {
  228. numper++;
  229. }
  230. notebook.number = numper;
  231. ENTER_DATA(&notebook);
  232. fseek(fPtr, notebook.number * sizeof(struct DATA), SEEK_SET);
  233. fwrite(&notebook, sizeof(struct DATA), 1, fPtr);
  234. }
  235.  
  236. void DELETE_RECORD(FILE* fPtr)
  237. {
  238. struct DATA notebook, blankClient = {0,"","","",0,0,0,"",0,0};
  239. int numper = 0;
  240. VIEW_DATA(fPtr);
  241. printf("Введите номер человека, данные которого нужно удалить из БД\n-> ");
  242. scanf("%d", &numper);
  243. notebook.number = numper;
  244. fseek(fPtr, notebook.number * sizeof(struct DATA), SEEK_SET);
  245. fread(&notebook, sizeof(struct DATA), 1, fPtr);
  246. if (notebook.number == 0)
  247. printf("Аккаунта с таким номером не существует\n");
  248. else {
  249. fseek(fPtr, notebook.number * sizeof(struct DATA), SEEK_SET);
  250. fwrite(&blankClient, sizeof(struct DATA), 1, fPtr);
  251. }
  252. }
  253.  
  254. void SEARCH_RECORD(FILE* readPtr)
  255. {
  256. struct DATA buf;
  257. char buf2[100];
  258. int choice;
  259. unsigned int search_mobile=1;
  260. bool flag = false;
  261. char search_surname[12] = { "0" }, search_name[10] = {"0"};
  262.  
  263. while (getchar() != '\n');
  264. printf("Осуществить поиск по:\n(1) -> Имени и Фамилии\n(2) -> Номеру телефона\n-> ");
  265. fgets(buf2, sizeof(buf2), stdin);
  266. /*scanf("%s", buf2);*/
  267. while (sscanf(buf2, "%d", &choice) != 1 || choice < 0 || choice > 2)
  268. {
  269. printf("Неверно введено значение. Попробуйте снова ->"); // выводим сообщение об ошибке
  270. fgets(buf2, sizeof(buf2), stdin);
  271. /*scanf("%s", buf2);*/ // считываем строку повторно
  272. }
  273. switch (choice)
  274. {
  275. case 1:
  276. printf("Имя-> ");
  277. while (getchar() != '\n');
  278. fgets(search_name, sizeof(search_name), stdin);
  279. search_name[strcspn(search_name, "\n")] = 0;
  280. printf("Фамилия-> ");
  281. fgets(search_surname, sizeof(search_surname), stdin);
  282. search_surname[strcspn(search_surname, "\n")] = 0;
  283. break;
  284. case 2:
  285. printf("Номер телефона-> ");
  286. search_mobile = CHECK_INTEGER(1, 99999999);
  287. break;
  288. }
  289. fseek(readPtr, 0 * sizeof(struct DATA), SEEK_SET);
  290. while (fread(&buf, sizeof(buf), 1, readPtr))
  291. {
  292. if ((strcmp(search_name, buf.name) == 0 && strcmp(search_surname, buf.surname) == 0) || search_mobile == buf.mobileNUMBER)
  293. {
  294. printf("%-2s| %-12s | %-10s | %-15s | %-13s | %-16s | %-11s |\n",
  295. "№", "Фамилия", "Имя", "Отчество", "Дата Рождения", "Адрес", "моб.телефон");
  296. printf("%-2d| %-12s | %-10s | %-15s | %02d.%02d.%04d | %-12s,%-3d | %-11d |\n",
  297. buf.number, buf.surname, buf.name, buf.patronymic,
  298. buf.BirthDate, buf.BirthMonth, buf.BirthYear,
  299. buf.addresSTREET, buf.addresHOME, buf.mobileNUMBER);
  300. flag = true;
  301. }
  302. }
  303. if (!flag)
  304. printf("Пользователь не найден...\n");
  305. }
  306.  
  307. void SORT_DATA(FILE* fPtr)
  308. {
  309. struct DATA blankClient, notebook= { 0,"","","",0,0,0,"",0,0 };
  310. int count_rec = 0;
  311.  
  312. while (fread(&notebook, sizeof(notebook), 1, fPtr) == 1)
  313. {
  314. count_rec++;
  315. }
  316.  
  317. for (int a = 0; a < count_rec; a++)
  318. {
  319. for (int b = 0; b < count_rec -1; b++)
  320. {
  321. fseek(fPtr, (b) * sizeof(struct DATA), SEEK_SET);
  322. fread(&notebook, sizeof(struct DATA), 1, fPtr);
  323. fseek(fPtr, (b+1) * sizeof(struct DATA), SEEK_SET);
  324. fread(&blankClient, sizeof(struct DATA), 1, fPtr);
  325. if (blankClient.number != 0)
  326. {
  327. if (strcmp(notebook.surname, blankClient.surname) > 0)
  328. {
  329. blankClient.number--;
  330. notebook.number++;
  331. fseek(fPtr, (b) * sizeof(struct DATA), SEEK_SET);
  332. fwrite(&blankClient, sizeof(struct DATA), 1, fPtr);
  333. fseek(fPtr, (b+1) * sizeof(struct DATA), SEEK_SET);
  334. fwrite(&notebook, sizeof(struct DATA), 1, fPtr);
  335. }
  336. }
  337. }
  338. }
  339. printf("Сортировка прошла успешно!\n");
  340. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.c:3:10: fatal error: windows.h: No such file or directory
 #include <windows.h>
          ^~~~~~~~~~~
compilation terminated.
stdout
Standard output is empty