From 23e2579e56bfe1b0ffc193eae089a8b1ab27ca68 Mon Sep 17 00:00:00 2001 From: Sambo Chea Date: Sun, 2 Aug 2020 13:57:55 +0700 Subject: [PATCH] Add write the data into file by linked list data --- bookNode | Bin 16992 -> 17120 bytes data/books.txt | 15 +++-- tests/bookNode.c | 140 +++++++++++++++++++++++++++++++++-------------- 3 files changed, 109 insertions(+), 46 deletions(-) diff --git a/bookNode b/bookNode index 1d2053be5f2a489887c9b39ec9012cc9cca763f6..81fa4aa8dd0a441757c0bc0f8a27c8ad1ff5b622 100755 GIT binary patch delta 3036 zcmZWreM}tJ5r1#*7ISAXci_t%wiE6`hyylv$C*#_wMSxSp>`k&B*Zl_q&aFgDO|$g zkT%wVY*JrHRLI0_R4FR0B3Fu>KO&UKO)Ya6%QSJNHmYj5mH(iTsvflyo7k@WfxVr1 zyL%A#t=`Pe%x~tsH}l@^n?J+Syv{$#C_A27VUP zv0JdO!k&pe1Bxw;d=rdV+&oAW<}DsxBIJhzZ6GTD6tP|iOL@EsoYnw$3tKKxP^?+L z-j@ak_Oi&(z8LH6k4E}f^p$;mv0m029gO{eC`7C;7fQ@n2;<^m%z<&K4X)dVla*K- zEL!t;2FUWO+zJO|H@q%q!K554YEu+}q?y~rn@&1c=vPmDDbL#8aZKK?6@3_BY%hTq zq8hfM-;zvAUg(DR9r^LL`xK_m&CI)q&7E;7Omj(yxi)?^=YdtfnVCJp`1E^5p zT(?no%qd`9v*`U8W5e|eevCWl>_AMa59i%=Mm`7oomFv%Iw${{jMzTQ^eXJ+S|ro! zk))Cwb;YEsAYC2lj*+f~bWfA6i*)ay?&OOk_L2DdT}@LHatB6L$K)r7jV1@zP7jmu z@*Pb(j(E%$QZ+s%KTLqcwj1i0t5CSmD{9WEiMiAw!ZC)FwoOF`j5+Khk-tVmiWRE3~7Y%=+ltjU+F)r`zO-==G30i2aB!*Kc$0r zr@`r|73=7xmFS{7+Adt3%fAda=XQIE)-4w2!R+OX?!uyBA}qpxF7*eri@lQi0_U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yyl&u9Uv??i0ce5@P}y55$RhL zzzxpBeBaMdapg?Vej0HPbF+x3;s_~J2Esy@AZbxgX;CLN>l(yu7)6upaau=FFt9Mw zeneYew{9T|?^gKx#0x&ra!g%$Nb&g&ippFCDfF=$+-%*Hb^d@gXr0iiqhU_etZ>G% zpuODHbrm%m?osEISdRgB!jR1_Ydfy*5KTPVA^n4@v;*c?g#LES^NN;-8jh$Mamqh+ z59%pD%g$r;aUoK=1m!9!E7(No@R6p5y+JspYZFBZQu>-?hGFn*c>}l87f}?KH`LcH z*%B6_9r^Y||W0RnHgmljc=WBdc{l&trdQh{Be+Z#k zzg_=JIqFed@h_zZ(X>ZY)U<{c4-VIMacsO$$ZyeKy9n$Em0R@FXoSx~wY(AThia|+ gX$T^vGoY@n%+gt|s@tHauH5o_g{s|y;l+jj08WWmrT_o{ diff --git a/data/books.txt b/data/books.txt index 16a8284..4eb15fe 100644 --- a/data/books.txt +++ b/data/books.txt @@ -1,6 +1,9 @@ -My First Book -Sambo Chea -892-928-990 -My Second Book -Chea Sambo -939-330-332 \ No newline at end of file +My Third Book +Zero 1 +000-000-111 +My Fouth Book +1 Zero +111-000-000 +My Fifth Book +2 Zero +222-000-000 \ No newline at end of file diff --git a/tests/bookNode.c b/tests/bookNode.c index afdf6d8..1d86278 100644 --- a/tests/bookNode.c +++ b/tests/bookNode.c @@ -17,14 +17,14 @@ BOOK *head; void add_book(char *title, char *author, char *isbn); void display(); int count(); -void save_book_data(struct book *selection); +void save_book_data(struct 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 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+"); @@ -35,42 +35,65 @@ int main() return EXIT_FAILURE; } - char title[20]; - char author[20]; - char isbn[20]; + // char title[20]; + // char author[20]; + // char isbn[20]; - while (!feof(file)) + // 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) { - 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); + 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(); @@ -81,16 +104,17 @@ int main() 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; + struct book *temp, *iterator; + temp = (struct book *)malloc(sizeof(struct book)); + temp->title = title; + temp->author = author; + temp->isbn = isbn; + // ref iterator = head; if (head == NULL) { - head = tempNode; + head = temp; head->next = NULL; } else @@ -99,8 +123,8 @@ void add_book(char *title, char *author, char *isbn) { iterator = iterator->next; } - tempNode->next = NULL; - iterator->next = tempNode; + temp->next = NULL; + iterator->next = temp; } } @@ -109,7 +133,8 @@ int count() int n = 1; BOOK *temp; temp = head; - if (head == NULL) { + if (head == NULL) + { return 0; } @@ -120,4 +145,39 @@ int count() } return n; +} + +void save_book_data(struct book *data) +{ + file = fopen("./data/books.txt", "a+"); + + if (file == NULL) + { + printf("Error: addressbook.dat could not be opened.\n"); + exit(EXIT_FAILURE); + } + + BOOK *temp; + temp = data; + + if (temp == NULL) + { + return; + } + + fputs(temp->title, file); + fputs(temp->author, file); + fputs(temp->isbn, file); + + while (temp->next != NULL) + { + temp = temp->next; + + fputs(temp->title, file); + fputs(temp->author, file); + fputs(temp->isbn, file); + } + + fflush(file); + fclose(file); } \ No newline at end of file