-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile_manager.c
More file actions
67 lines (59 loc) · 1.78 KB
/
file_manager.c
File metadata and controls
67 lines (59 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "list.h"
int getStringSize(char *string) {
int i = 0;
while (string[i] != '\0') {
i++;
}
return i + 1;
}
char *initStringField(char *string) {
int len = getStringSize(string);
char *str = (char *) malloc(len * sizeof(char));
strcpy(str, string);
return str;
}
void readListFromFile(FILE *file, List *list) {
if (file != NULL) {
char scanner[1024];
while (fgets(scanner, 1024, file)) {
ShopItem *item = malloc(sizeof(ShopItem));
int count = 0;
char *words = strtok(scanner, ";");
while (words != NULL) {
if(count == 0) {
item->name = initStringField(words);
}
else if(count == 1) {
item->type = initStringField(words);
}
else if(count == 2) {
item->description = initStringField(words);
}
else if(count == 3) {
item->count = atoi(words);
}
else if(count == 4) {
item->price = atof(words);
}
else if(count == 5) {
item->stars = atof(words);
}
count++;
words = strtok(NULL, ";,");
}
add(item, list);
}
}
}
void saveListToFile(FILE *file, List *list) {
ShopItem *current = list->head;
if (current == NULL)
return;
while (current != NULL) {
fprintf(file, "%s;%s;%s;%d;%lf;%lf\n", current->name, current->type, current->description, current->count, current->price, current->stars);
current = (ShopItem *) current->next;
}
}