Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #define MAX_COUNT 100
- #define TITLE_SIZE 50
- #define AUTOR_SIZE 50
- void addBook();
- void removeBook();
- void printBooks();
- void readFromFile();
- void writeToFile();
- void trim(char*);
- typedef struct
- {
- char title[TITLE_SIZE];
- char autor[AUTOR_SIZE];
- int pages;
- } Book;
- Book library[MAX_COUNT];
- int count = 0;
- int main()
- {
- char option;
- while (1)
- {
- printf("1. Add new book\n");
- printf("2. Remove book at given number\n");
- printf("3. Print books\n");
- printf("4. Insert books from file\n");
- printf("5. Save books to file\n");
- printf("6. Exit\n");
- printf("-> Enter option: ");
- option = getch();
- switch (option)
- {
- case '1': addBook(); break;
- case '2': removeBook(); break;
- case '3': printBooks(); break;
- case '4': readFromFile(); break;
- case '5': writeToFile(); break;
- case '6': return 0;
- }
- getch(); // pause
- }
- return 0;
- }
- void addBook()
- {
- char pages[10];
- printf("\nEnter book title: ");
- fgets(library[count].title, TITLE_SIZE, stdin);
- trim(library[count].title);
- printf("Enter book autor: ");
- fgets(library[count].autor, AUTOR_SIZE, stdin);
- trim(library[count].autor);
- printf("Enter pages: ");
- fgets(pages, 10, stdin);
- library[count].pages = atoi(pages);
- count++;
- fflush(stdin);
- }
- void removeBook()
- {
- int num, i;
- printf("\nEnter book number: ");
- scanf("%d", &num);
- if (num < 0 || num >= count)
- {
- printf("No such book!");
- return;
- }
- for (i = num; i < count - 1; i++)
- {
- library[i] = library[i + 1];
- }
- count--;
- fflush(stdin);
- }
- void printBooks()
- {
- int i;
- printf("\n ---> Books list <---\n");
- for (i = 0; i < count; i++)
- {
- printf("-- Book number %d --\n", i);
- printf("Title: %s\n", library[i].title);
- printf("Autor: %s\n", library[i].autor);
- printf("Pages: %d\n", library[i].pages);
- printf("-------------\n");
- }
- }
- void readFromFile()
- {
- char filename[30], pages[10];
- FILE *libFile;
- fflush(stdin);
- printf("\nEnter file to read books: ");
- fgets(filename, 30, stdin);
- trim(filename);
- if (!(libFile = fopen(filename, "r")))
- {
- printf("File problem!");
- return;
- }
- while (!feof(libFile))
- {
- fgets(library[count].title, TITLE_SIZE, libFile);
- trim(library[count].title);
- fgets(library[count].autor, AUTOR_SIZE, libFile);
- trim(library[count].autor);
- fgets(pages, 10, libFile);
- library[count].pages = atoi(pages);
- count++;
- }
- fflush(stdin);
- fclose(libFile);
- }
- void writeToFile()
- {
- int i;
- char filename[30], pages[10];
- FILE *libFile;
- printf("\nEnter file to write books: ");
- fgets(filename, 30, stdin);
- trim(filename);
- if (!(libFile = fopen(filename, "w")))
- {
- printf("File problem!");
- return;
- }
- for (i = 0; i < count; i++)
- {
- fprintf(libFile, "%s\n", library[i].title);
- fprintf(libFile, "%s\n", library[i].autor);
- fprintf(libFile, "%d\n", library[i].pages);
- }
- fflush(stdin);
- fclose(libFile);
- }
- // remove new line symbol after fgets
- void trim(char *str)
- {
- str[strlen(str) - 1] = '\0';
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement