Загрузка данных
#define _CRT_SECURE_NO_WARNINGS
#include "funcs.h"
#include <stdio.h>
#include <string.h>
int read_file(const char *file_name) {
FILE *file = fopen(file_name, "r+b");
if (!file) {
printf("Файл не найден.\n");
return 0;
}
fseek(file, 0, SEEK_END);
int length = ftell(file) / sizeof(struct Row);
rewind(file);
struct Row row;
for (int i = 0; i < length; i++) {
fread(&row, sizeof(struct Row), 1, file); // Читаем всё вместе иначе не работало, написало что компилятор padding делает
printf("row id: %d; row value: %s\n", row.id, row.value);
}
fclose(file);
return 0;
}
int add_value(const char *file_name) {
struct Row new_row;
// получить последнее id или первое дать
FILE *file_length = fopen(file_name, "r+b");
int length = 0;
if (file_length) {
fseek(file_length, 0, SEEK_END);
length = ftell(file_length) / sizeof(struct Row);
if (length >= 1) {
// Читаем последнюю запись только если файл не пустой
fseek(file_length, (length - 1) * sizeof(struct Row), SEEK_SET);
fread(&new_row, sizeof(struct Row), 1, file_length);
new_row.id = new_row.id + 1;
} else {
new_row.id = 1; // файл есть но пустой
}
} else {
printf("Файл не существует, будет создан новый.\n");
new_row.id = 1; // файла нет
}
// ввести значение без '\n'
printf("Введите новое значение для ввода: ");
fgets(new_row.value, 50, stdin);
size_t len = strlen(new_row.value);
if (len > 0 && new_row.value[len - 1] == '\n') {
new_row.value[len - 1] = '\0';
}
// ввести значение в конец файла
FILE *file = fopen(file_name, "ab");
if (!file) {
printf("Ошибка открытия файла\n");
return 1;
}
fwrite(&new_row, sizeof(struct Row), 1, file);
fclose(file);
printf("Добавлена запись: ID=%d, Value=%s\n", new_row.id, new_row.value);
return 0;
}
int delete_value(const char *file_name) {
FILE *file_r = fopen(file_name, "r+b");
if (!file_r) {
printf("Файл не найден.\n");
return 0;
}
fseek(file_r, 0, SEEK_END);
int length = ftell(file_r) / sizeof(struct Row);
rewind(file_r);
// получить значение на удаление
char find[50];
printf("Введите значение на удаление: ");
fgets(find, 50,stdin);
size_t len = strlen(find);
if (len > 0 && find[len - 1] == '\n') {
find[len - 1] = '\0';
}
// пройтись по файлу и записать в структуру с флагом значения
struct Row_Delete rows_d[10];
struct Row temp_r;
int counter = 0;
for (int i = 0; i < length; i++) {
fread(&temp_r, sizeof(struct Row), 1, file_r);
rows_d[i].id = temp_r.id;
strcpy(rows_d[i].value, temp_r.value);
if (strcmp(rows_d[i].value, find) == 0) {
rows_d[i].deleted = 1;
counter++;
}
else {
rows_d[i].deleted = 0;
rows_d[i].id -= counter;
}
}
fclose(file_r);
// очистить старый файл
remove(file_name);
// заменить файл на значения структуры с флагом без помеченных значений
FILE *file_w = fopen(file_name, "wb");
for (int i = 0; i < length; i++) {
if (rows_d[i].deleted != 1) {
temp_r.id = rows_d[i].id;
strcpy(temp_r.value, rows_d[i].value);
fwrite(&temp_r, sizeof(struct Row), 1, file_w);
}
}
fclose(file_w);
if (counter > 0) {
printf("Значение {%s} удалено {%d} раз.\n", find, counter);
}
else {
printf("Значения {%s} нет в файле.\n", find);
}
return 0;
}
int find_value(const char *file_name) {
FILE *file = fopen(file_name, "r+b");
if (!file) {
printf("Файл не найден.\n");
return 0;
}
fseek(file, 0, SEEK_END);
int length = ftell(file) / sizeof(struct Row);
rewind(file);
// получение значения для поиска
char find[50];
printf("Введите новое значение для поиска: ");
fgets(find, 50, stdin);
size_t len = strlen(find);
if (len > 0 && find[len - 1] == '\n') {
find[len - 1] = '\0';
}
// поиск
struct Row row;
int counter = 0;
for (int i = 0; i < length; i++) {
fread(&row, sizeof(struct Row), 1, file);
if (strcmp(find, row.value) == 0) {
counter++;
}
}
if (counter > 0) {
printf("Значение {%s} есть в файле, оно встречается {%d}.", find, counter);
} else {
printf("Значение {%s} нет в файле.", find);
}
return 0;
}