Created
January 24, 2017 12:05
-
-
Save shkesar/4eca77cf04448391a8f766737527aa80 to your computer and use it in GitHub Desktop.
Library
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <assert.h> | |
#include <string.h> | |
#define MAX 100 | |
#define EXIT_CHOICE 10 | |
#define USERS_FILE "users.txt" | |
#define BOOKS_FILE "books.txt" | |
struct customer { | |
char fname[MAX]; | |
char lname[MAX]; | |
int id; | |
}; | |
typedef struct customer customer_t; | |
struct book{ | |
char title[MAX]; | |
char author[MAX]; | |
int id; | |
}; | |
typedef struct book book_t; | |
int customer_count = 0; | |
int book_count = 0; | |
int cust_id = 1; | |
int book_id = 1; | |
int get_input () { | |
int c; | |
printf( | |
"Choose from the following options :\n" | |
"1. Display all registered users\n" | |
"2. Display all books\n" | |
"3. Display all borrowers\n" | |
"4. Add a new user\n" | |
"5. Add a new book\n" | |
"6. Issue a new book\n" | |
"7. Accept back a book\n" | |
"8. Generate fine\n" | |
"10. Exit\n\n\n" | |
); | |
scanf("%d", &c); | |
return c; | |
} | |
void display_users() { | |
FILE *file = fopen(USERS_FILE, "rb"); | |
customer_t customers[MAX]; | |
fread(customers, sizeof(customer_t), customer_count, file); | |
for (int i = 0; i < customer_count; i++) { | |
printf("%d\t%s\t%s\n", customers[i].id, customers[i].fname, customers[i].lname); | |
} | |
fclose(file); | |
} | |
void add_user() { | |
printf("Enter name:\n"); | |
customer_t customer; | |
if (fgets(customer.fname, MAX, stdin) != NULL) { | |
customer.id = cust_id++; | |
FILE *file = fopen(USERS_FILE, "wb"); | |
fwrite(&customer, sizeof(customer), 1, file); | |
fclose(file); | |
} | |
} | |
void display_books(){ | |
FILE *file = fopen(BOOKS_FILE,"rb"); | |
book_t books[MAX]; | |
fread(books,sizeof(book_t), book_count, file); | |
for(int i = 0 ; i<book_count ; i++){ | |
printf("%d | %s | %s ",books[i].id,books[i].title,books[i].author); | |
} | |
} | |
void add_book(){ | |
printf("Enter title and Author\n"); | |
book_t book; | |
if(fgets(book.title, MAX, stdin) != NULL || fgets(book.author, MAX, stdin) != NULL){ | |
book.id = book_id++; | |
FILE *file = fopen(BOOKS_FILE, "wb"); | |
fwrite(&book, sizeof(book), 1, file); | |
} | |
} | |
int main () { | |
int choice; | |
while ((choice = get_input()) != EXIT_CHOICE) { | |
switch (choice) { | |
case 1: display_users(); | |
break; | |
case 2: display_books(); | |
break; | |
case 3: add_book(); | |
break; | |
case 4: add_user(); | |
break; | |
} | |
} | |
printf("Saving data..\n"); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment