Skip to content

Instantly share code, notes, and snippets.

@shkesar
Created January 24, 2017 17:49
Show Gist options
  • Save shkesar/b53fd24ffc6d01671d2671b751bc0e52 to your computer and use it in GitHub Desktop.
Save shkesar/b53fd24ffc6d01671d2671b751bc0e52 to your computer and use it in GitHub Desktop.
#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 user_count = 0;
int book_count = 0;
FILE *user_file, *book_file;
int get_count(FILE *input) {
int pos = ftell(input);
rewind(input);
int count = fread(&count, sizeof(int), 1, input);
fseek(input, pos, SEEK_SET);
return count;
}
void set_count(FILE *input, int count) {
int pos = ftell(input);
rewind(input);
fwrite(&count, sizeof(int), 1, input);
fseek(input, pos, SEEK_SET);
}
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);
int a; // exhaust scanf's "\n" from stdin
while ((a = getchar()) != EOF && a != '\n')
continue;
return c;
}
void display_users() {
customer_t customers[MAX];
int pos = ftell(user_file);
rewind(user_file);
fread(customers, sizeof(customer_t), user_count, user_file);
printf("%d elements present\n", user_count);
for (int i = 0; i < user_count; i++) {
printf("%d\t%s\t%s\n", customers[i].id, customers[i].fname, customers[i].lname);
}
fseek(user_file, pos, SEEK_SET);
}
void add_user() {
customer_t customer;
printf("Enter name:\n");
fgets(customer.fname, MAX, stdin);
if (customer.fname != NULL) {
customer.id = user_count++;
fwrite(&customer, sizeof(customer), 1, user_file);
}
set_count(user_file, user_count);
}
void display_books(){
book_t books[MAX];
int pos = ftell(book_file);
rewind(book_file);
fread(books,sizeof(book_t), book_count, book_file);
printf("%d elements present\n", book_count);
for(int i = 0 ; i<book_count ; i++){
printf("%d | %s | %s ",books[i].id,books[i].title,books[i].author);
}
fseek(book_file, pos, SEEK_SET);
}
void add_book(){
book_t book;
printf("Enter title: ");
fgets(book.title, MAX, stdin);
printf("Enter author: ");
fgets(book.author, MAX, stdin);
if(book.title != NULL){
book.id = book_count++;
fwrite(&book, sizeof(book), 1, book_file);
}
}
int main () {
int choice;
user_file = fopen(USERS_FILE, "rwb");
book_file = fopen(BOOKS_FILE, "rwb");
while ((choice = get_input()) != EXIT_CHOICE) {
switch (choice) {
case 1: display_users();
break;
case 2: display_books();
break;
case 4: add_user();
break;
case 5: add_book();
break;
}
}
fclose(user_file);
fclose(book_file);
printf("Saving data..\n");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment