Archived
1

Implemented Writing Files

Implemented Writing Files with user-inputed path. Or something like this. idk
This commit is contained in:
ertz4 2020-01-23 15:16:56 +01:00
parent 467e308cb6
commit b22a0977aa

View File

@ -53,7 +53,7 @@ void show_insert(Node** list)
list_insert(list_get(*list, index), book_create(title, author, year));
}
void show_print(Node** list)
void show_print(Node** list)
{
list_print(*list, &book_print);
}
@ -63,7 +63,7 @@ void show_get(Node** list)
int index;
printf("Enter place: ");
scanf_s(" %d", &index);
list_print(list_get(*list,index), &book_print);
list_print(list_get(*list, index), &book_print);
}
void show_remove(Node** list)
@ -76,7 +76,7 @@ void show_remove(Node** list)
void show_read(Node** list)
{
char path[101], line[256], *title, *author, *year;
char path[101], line[256], * title, * author, * year;
FILE* CSV;
printf("Enter the path to the file of haram: ");
@ -95,7 +95,7 @@ void show_read(Node** list)
title = strtok(line, ";");
author = strtok(NULL, ";");
year = strtok(NULL, ";");
printf("%s, %s, %s\n", title, author, year);
list_push(list, book_create(title, author, atoi(year)));
@ -105,6 +105,32 @@ void show_read(Node** list)
fclose(CSV);
}
void show_write(Node** list)
{
char path[101], line[256];
FILE* CSV;
Node* node = *list;
Book* book;
printf("Enter the path to the file_s: ");
gets_s(path, 101);
if (!(CSV = fopen(path, "w")))
{
puts("Failed to read file!\n");
return;
}
while (node)
{
book = (Book*)node->data;
fprintf(CSV, "%s;%s;%d\n", book->title, book->author, book->year);
node = node->next;
}
fclose(CSV);
}
void main(void)
{
Node* list = NULL;
@ -117,6 +143,7 @@ void main(void)
{"Look at item by index", '5', (void*)&show_get, &list},
{"Remove item by index", '6', (void*)&show_remove, &list},
{"READ", 'r', (void*)&show_read, &list},
{"RIDE", 'w', (void*)&show_write, &list},
{"BLANK", ' ', NULL, NULL},
{"Quit", 'q', (void*)&exit, NULL}
};