From d4887d22ba45183bbab3d40d542c5d60ddf9b6f0 Mon Sep 17 00:00:00 2001 From: Sambo Chea Date: Sun, 2 Aug 2020 13:21:17 +0700 Subject: [PATCH] Add book node store in local txt as database and its concepts --- bookNode | Bin 0 -> 16992 bytes data/books.txt | 6 +++ tests/bookNode.c | 123 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 129 insertions(+) create mode 100755 bookNode create mode 100644 data/books.txt create mode 100644 tests/bookNode.c diff --git a/bookNode b/bookNode new file mode 100755 index 0000000000000000000000000000000000000000..1d2053be5f2a489887c9b39ec9012cc9cca763f6 GIT binary patch literal 16992 zcmeHOeQX@X6`#9{laPb4=*-8r-JEvcyx;qnnYXiZJG(RA2(`C*Js!cyD{d9U9jP)Uq|QQ|-6&a&trv@h zDXtUOiupk1;V`9ysLCo4UM1&#FGgLQMbiA30Q2sHk@R(srO{VRV z6M`w*-2ppUPc8o>!eQT`%bP1M97m?QxuU*5KIqgZ9~g+n(!<>e=f3)Z zXm|ZUB#?|#9jJa(7TImx-i5Oe)Ymx7lwX8n0S?l=3`cPyThb?8>NC_%jx)*lK=$8G z)zG}Wgm&E=_?2_u>*l~$&VgS$2c86O;3!V@07}(!@f^57^m`Edz&gJ`0C9H%}9NN>+TJn4wR@W4R4N7y!**~wHmk+KKF(U{oU-qzG?-xRnhu=W$K zHH`ZP<1uY)qh4OuPjPgO=%@Muh)7=(#ijNNix{Nr7E@BXw%n%)n0DdRM>y4*rpyvX zemNN~ocau>Iu}m;h*N_L*F%*Qt#{$>`^t3T+y}|Pa^a==Fqc=Rw)30_6yl zBT$Y&IRfPfxFhgE<#qqCM&DUz9jutbv%j@}Jmt+zSfjsOcv7n6*1irnx9Sa?D_5Av z5#LX;sk1qxRlg^m9*L(WB>yt;UgF0k|04192t4()Eq(P_Vxd2`S5=SW0Nt=@H<1i7=lY_RJjwQ|P6F_L##51y9} zPF(WAftATxCnmR8o>SIKmr~2}-WIdB$`xYQNw{ngVr28*X(pep?tS*-2V&I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z;`tKqe=gQ%p6SnEi>Ir+JwHch*8oIfJ;lxNjQ2F7xjoL*;9Ah6UTN%p)p8x|H0M`>v9x^CQ|} vRtKKOak?a2KG%!hM{(Vir8*DatZ1UhIhV`2)yR~xe|v+{xWI~<7Tf;|(>263 literal 0 HcmV?d00001 diff --git a/data/books.txt b/data/books.txt new file mode 100644 index 0000000..16a8284 --- /dev/null +++ b/data/books.txt @@ -0,0 +1,6 @@ +My First Book +Sambo Chea +892-928-990 +My Second Book +Chea Sambo +939-330-332 \ No newline at end of file diff --git a/tests/bookNode.c b/tests/bookNode.c new file mode 100644 index 0000000..afdf6d8 --- /dev/null +++ b/tests/bookNode.c @@ -0,0 +1,123 @@ +#include +#include + +struct book +{ + char *title; + char *author; + char *isbn; + struct book *next; +}; + +typedef struct book BOOK; + +FILE *file; +BOOK *head; + +void add_book(char *title, char *author, char *isbn); +void display(); +int count(); +void save_book_data(struct book *selection); + +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 Fouth Book", "1 Zero", "111-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); + } + + // close the file + fclose(file); + + // display(); + + int countEntries = count(); + printf("There are %d entries\n", countEntries); + + return EXIT_SUCCESS; +} + +void add_book(char *title, char *author, char *isbn) +{ + struct book *tempNode, *iterator; + tempNode = (struct book *)malloc(sizeof(struct book)); + tempNode->title = title; + tempNode->author = author; + tempNode->isbn = isbn; + iterator = head; + + if (head == NULL) + { + head = tempNode; + head->next = NULL; + } + else + { + while (iterator->next != NULL) + { + iterator = iterator->next; + } + tempNode->next = NULL; + iterator->next = tempNode; + } +} + +int count() +{ + int n = 1; + BOOK *temp; + temp = head; + if (head == NULL) { + return 0; + } + + while (temp->next != NULL) + { + n++; + temp = temp->next; + } + + return n; +} \ No newline at end of file