Updated book store data and display it and add it

This commit is contained in:
Sambo Chea 2020-08-02 14:48:59 +07:00
parent 171e2e38e6
commit ba3a1df3a4
3 changed files with 146 additions and 73 deletions

View File

@ -6,4 +6,20 @@ My Fouth Book
111-000-000
My Fifth Book
2 Zero
222-000-000My Third Book
Zero 1
000-000-111
My Fouth Book
1 Zero
111-000-000
My Fifth Book
2 Zero
222-000-000My Third Book
Zero 1
000-000-111
My Fouth Book
1 Zero
111-000-000
My Fifth Book
2 Zero
222-000-000

3
runBook Executable file
View File

@ -0,0 +1,3 @@
#!/bin/bash
gcc tests/bookNode.c -o bookNode.bin && ./bookNode.bin

View File

@ -17,88 +17,24 @@ BOOK *head;
void add_book(char *title, char *author, char *isbn);
void display();
int count();
void add_book_data();
void save_book_data(struct book *data);
void read_file_to_data();
void print_data(BOOK *data);
int main()
{
head = NULL;
add_book("My Third Book", "Zero 1", "000-000-111");
add_book("My Fouth Book", "1 Zero", "111-000-000");
add_book("My Fifth Book", "2 Zero", "222-000-000");
int i;
file = fopen("./data/books.txt", "a+");
if (file == NULL)
{
printf("Error: addressbook.dat could not be opened.\n");
return EXIT_FAILURE;
}
// char title[20];
// char author[20];
// char isbn[20];
// while (!feof(file))
// {
// fgets(title, 20, file);
// fgets(author, 20, file);
// fgets(isbn, 20, file);
// // Removes newline characters from the ends of the names
// i = 0;
// while (title[i] != '\n')
// {
// i++;
// }
// title[i] = '\0';
// i = 0;
// while (author[i] != '\n')
// {
// i++;
// }
// author[i] = '\0';
// // Adds the entry from the strings with the file data in them
// add_book(title, author, isbn);
// }
BOOK *temp;
temp = head;
fputs(temp->title, file);
fputs("\n", file);
fputs(temp->author, file);
fputs("\n", file);
fputs(temp->isbn, file);
while (temp->next != NULL)
{
temp = temp->next;
fputs("\n", file);
fputs(temp->title, file);
fputs("\n", file);
fputs(temp->author, file);
fputs("\n", file);
fputs(temp->isbn, file);
}
// close the file
fclose(file);
// save book data
// save_book_data(head);
// display();
int countEntries = count();
printf("There are %d entries\n", countEntries);
// add book data
add_book_data();
// display the books
display();
return EXIT_SUCCESS;
}
@ -147,6 +83,45 @@ int count()
return n;
}
void add_book_data()
{
add_book("My Third Book", "Zero 1", "000-000-111");
add_book("My Fouth Book", "1 Zero", "111-000-000");
add_book("My Fifth Book", "2 Zero", "222-000-000");
int i;
file = fopen("./data/books.txt", "a+");
if (file == NULL)
{
printf("Error: addressbook.dat could not be opened.\n");
exit(EXIT_FAILURE);
}
BOOK *temp;
temp = head;
fputs(temp->title, file);
fputs("\n", file);
fputs(temp->author, file);
fputs("\n", file);
fputs(temp->isbn, file);
while (temp->next != NULL)
{
temp = temp->next;
fputs("\n", file);
fputs(temp->title, file);
fputs("\n", file);
fputs(temp->author, file);
fputs("\n", file);
fputs(temp->isbn, file);
}
// close the file
fclose(file);
}
void save_book_data(struct book *data)
{
file = fopen("./data/books.txt", "a+");
@ -180,4 +155,83 @@ void save_book_data(struct book *data)
fflush(file);
fclose(file);
}
void read_file_to_data()
{
// clear head in linked list
head = NULL;
FILE *file;
file = fopen("./data/books.txt", "r");
if (file == NULL)
{
printf("cannot read file!");
exit(EXIT_FAILURE);
}
char title[20];
char author[20];
char isbn[20];
int i;
while (!feof(file))
{
fgets(title, 20, file);
fgets(author, 20, file);
fgets(isbn, 20, file);
i = 0;
while (title[i] != '\n')
{
i++;
}
title[i] = '\0';
i = 0;
while (author[i] != '\n')
{
i++;
}
author[i] = '\0';
add_book(title, author, isbn);
}
fclose(file);
}
void display()
{
read_file_to_data();
print_data(head);
}
void print_data(BOOK *book)
{
BOOK *temp;
temp = book;
if (temp == NULL)
{
printf("book is empty!");
return;
}
printf("Book title: %s\n", temp->title);
printf("Book author: %s\n", temp->author);
printf("Book isbn: %s", temp->isbn);
while (temp->next != NULL)
{
temp = temp->next;
printf("\nBook title: %s\n", temp->title);
printf("Book title: %s\n", temp->title);
printf("Book title: %s", temp->title);
}
}