-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSource.c
66 lines (59 loc) · 2.38 KB
/
Source.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#define _CRT_SECURE_NO_WARNINGS
#include "Header.h"
#include "Structs.h"
#include "funcs.h"
int main() {
setlocale(LC_ALL, "en_US.UTF-8");
int count = 0;
CarOwner* owners = NULL;
int choose = 0;
int CarOwner_id = 0;
FILE* file = NULL;
errno_t err;
err = fopen_s(&file, "owners.bin", "rb");
if (err == 0) {
fread(&count, sizeof(int), 1, file); // чтение count
fread(&CarOwner_id, sizeof(int), 1, file); // чтение значения id
owners = (CarOwner*)malloc(count * sizeof(CarOwner));
fread(owners, sizeof(CarOwner), count, file);
for (int i = 0; i < count; i++) {
if (owners[i].id > CarOwner_id) {
CarOwner_id = owners[i].id;
}
}
fclose(file);
}
else {
// если файл не существует, создаем пустой массив владельцев
owners = (CarOwner*)malloc(sizeof(CarOwner));
}
do
{
printf("1: Добавить владельца машины\n2: Удалить запись\n3: Вывести все записи\n4: Отредактировать запись\n5: Отсортировать владельцев автомобилей в алфавитном порядке\n6: Поиск\n7: Вывести отсортированный файл\n8: Вывести поисковый файл\n0: Выйти из программы");
if (scanf_s("%d", &choose) != 1) {
fflush(stdin);
continue;
}
switch (choose)
{
case 1: owners = add_owner(owners, &count, CarOwner_id);break;
case 2: owners = delete_owner(owners, &count); break;
case 3: print_owners(owners, count); break;
case 4: edit_owner(owners, &count); break;
case 5: sort_ownersAZ(owners, count); break;
case 6: search_owners(owners, count); break;
case 7: print_sorted_list("sort_owners_az.bin"); break;
case 8: print_sorted_list("search.bin"); break;
default:
break;
}
} while (choose != 0);
file = fopen("owners.bin", "wb");
if (file != NULL) {
fwrite(&count, sizeof(int), 1, file);
fwrite(&CarOwner_id, sizeof(int), 1, file);
fwrite(owners, sizeof(CarOwner), count, file);
fclose(file);
}
return 0;
}