Add write the data into file by linked list data

This commit is contained in:
Sambo Chea 2020-08-02 13:57:55 +07:00
parent d4887d22ba
commit 23e2579e56
3 changed files with 109 additions and 46 deletions

BIN
bookNode

Binary file not shown.

View File

@ -1,6 +1,9 @@
My First Book My Third Book
Sambo Chea Zero 1
892-928-990 000-000-111
My Second Book My Fouth Book
Chea Sambo 1 Zero
939-330-332 111-000-000
My Fifth Book
2 Zero
222-000-000

View File

@ -17,14 +17,14 @@ BOOK *head;
void add_book(char *title, char *author, char *isbn); void add_book(char *title, char *author, char *isbn);
void display(); void display();
int count(); int count();
void save_book_data(struct book *selection); void save_book_data(struct book *data);
int main() int main()
{ {
head = NULL; head = NULL;
add_book("My Third Book", "Zero 1", "000-000-111"); 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 Fouth Book", "1 Zero", "111-000-000"); add_book("My Fifth Book", "2 Zero", "222-000-000");
int i; int i;
file = fopen("./data/books.txt", "a+"); file = fopen("./data/books.txt", "a+");
@ -35,42 +35,65 @@ int main()
return EXIT_FAILURE; return EXIT_FAILURE;
} }
char title[20]; // char title[20];
char author[20]; // char author[20];
char isbn[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); temp = temp->next;
fgets(author, 20, file); fputs("\n", file);
fgets(isbn, 20, file); fputs(temp->title, file);
fputs("\n", file);
// Removes newline characters from the ends of the names fputs(temp->author, file);
i = 0; fputs("\n", file);
fputs(temp->isbn, file);
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 // close the file
fclose(file); fclose(file);
// save book data
// save_book_data(head);
// display(); // display();
int countEntries = count(); int countEntries = count();
@ -81,16 +104,17 @@ int main()
void add_book(char *title, char *author, char *isbn) void add_book(char *title, char *author, char *isbn)
{ {
struct book *tempNode, *iterator; struct book *temp, *iterator;
tempNode = (struct book *)malloc(sizeof(struct book)); temp = (struct book *)malloc(sizeof(struct book));
tempNode->title = title; temp->title = title;
tempNode->author = author; temp->author = author;
tempNode->isbn = isbn; temp->isbn = isbn;
// ref
iterator = head; iterator = head;
if (head == NULL) if (head == NULL)
{ {
head = tempNode; head = temp;
head->next = NULL; head->next = NULL;
} }
else else
@ -99,8 +123,8 @@ void add_book(char *title, char *author, char *isbn)
{ {
iterator = iterator->next; iterator = iterator->next;
} }
tempNode->next = NULL; temp->next = NULL;
iterator->next = tempNode; iterator->next = temp;
} }
} }
@ -109,7 +133,8 @@ int count()
int n = 1; int n = 1;
BOOK *temp; BOOK *temp;
temp = head; temp = head;
if (head == NULL) { if (head == NULL)
{
return 0; return 0;
} }
@ -120,4 +145,39 @@ int count()
} }
return n; 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);
} }