#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <windows.h>
#include <io.h>
#include <locale>
struct Book
{
char Author[100];//Автор
char Title[100];//Название
int god;//год издания
};
void PrintBookInFile(FILE *);//Функция которая печатает книги из файла на экран.
void PrintBook(Book *);//Функция которая печатает определеную книгу на экран из файла.
void FindByAuthorAndTitle(FILE *);//Функция которая находит книги по автору и названию из файла и выводит их на экран.
void AddBookToFile(FILE *);//Функция которая добавляет книгу в файл.
void FindByAuthor(FILE *);//Функция которая находит книги по автору из файла и выводит их на экран.
void main()
{
setlocale(LC_ALL, "rus");
FILE *file;
const char *fname = "library.bin";
int kamand;
if (!(file = fopen(fname, "rb"))){file = fopen(fname, "wb");}
fclose(file);
do
{ printf("Добро пожаловать в базу библиотеки,выберите действие которое хотите совершить:\n");
printf("1) Добавить книгу\n2) Вывести все книги\n3) Поиск по автору\n4) Поиск по автору и названию\n0) Выход\n");
fflush(stdin); scanf("%d", &kamand);
system("cls");
switch(kamand)
{
case 1:file = fopen(fname, "r+b");
if (!file){return;}
AddBookToFile(file);fclose(file);break;
case 2:file = fopen(fname, "rb");
if (!file){return;}
PrintBookInFile(file);fclose(file);break;
case 3:file = fopen(fname, "rb");
if (!file){return;}
FindByAuthor(file);fclose(file);break;
case 4:file = fopen(fname, "rb");
if (!file){return;}
FindByAuthorAndTitle(file);fclose(file);break;
case 0:return;}
}while (kamand);
}
void PrintBook(Book *book)
{
printf("Название книги: %s Автор книги: %s Год издания: %d\n", book->Title, book->Author, book->god);
}
void PrintBookInFile(FILE *file)
{
Book book;
while (!feof(file))
{
fread(&book, sizeof(Book), 1, file);
if (feof(file)){break;}
PrintBook(&book);
}
}
void FindByAuthorAndTitle(FILE *file)
{
char author[80];
char title[80];
Book book;
printf("Введите автора для поиска: "); fflush(stdin); gets(author);
printf("Введите название книги: "); fflush(stdin); gets(title);
while (!feof(file))
{
fread(&book, sizeof(Book), 1, file);
if (feof(file)){break;}
if (!strcmp(book.Author, author) && !strcmp(book.Title, title))
{
PrintBook(&book);
}
}
}
void AddBookToFile(FILE *file)
{
Book book, book2;//book2- временная переменная для работы с книгами.
fpos_t begin, current, length;
printf("Введите название книги: "); fflush(stdin); gets(book.Title);
printf("Введите автора: "); fflush(stdin); gets(book.Author);
printf("Введите год издания: "); fflush(stdin); scanf("%d", &book.god);
//длина файла
length = filelength(fileno(file));
//поиск автора
while(!feof(file))
{
fread(&book2, sizeof(Book), 1, file);
if (feof(file)){break;}
if (strcmp(book.Author, book2.Author) <= 0)
{break;}
}
//позия указателя в файле
fgetpos(file, &begin);
//анализируем по какому условию вышли из файла и при необходимости сдвигаем указатель назад
if (begin != length || (begin == length && strcmp(book.Author, book2.Author) <= 0))
{begin = begin ? begin - sizeof(Book) : begin;}
fseek(file , begin ,SEEK_SET);
//ишем по автору и название книги
while(!feof(file))
{
fread(&book2, sizeof(Book), 1, file);
if (feof(file))
{break;}
if (strcmp(book.Author, book2.Author) != 0 || strcmp(book.Title, book2.Title) <= 0)
{break;}
}
fgetpos(file, &begin);
//анализируем
if (begin != length || (begin == length && (strcmp(book.Author, book2.Author) < 0 || (strcmp(book.Author, book2.Author) == 0 && strcmp(book.Title, book2.Title) < 0))) )
{begin = begin ? begin - sizeof(Book) : begin;}
fseek(file , begin ,SEEK_SET);
//вставка
do
{
fread(&book2, sizeof(Book), 1, file);
if (feof(file) || begin >= length)
{break;}
fgetpos(file, &begin);
begin = begin - sizeof(Book);
fseek(file , begin ,SEEK_SET);
fwrite(&book, sizeof(Book), 1, file);
fgetpos(file, &begin);
book = book2;
}while (begin < length);
fwrite(&book, sizeof(Book), 1, file);
}
void FindByAuthor(FILE *file)
{
char author[80];
Book book;
printf("Введите автора для поиска: "); fflush(stdin); gets(author);
while (!feof(file))
{
fread(&book, sizeof(Book), 1, file);
if (feof(file)){break;}
if (!strcmp(book.Author, author))
{
PrintBook(&book);
}
}
}