-
Notifications
You must be signed in to change notification settings - Fork 0
/
io.c
117 lines (100 loc) · 2.22 KB
/
io.c
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include <stdlib.h>
#include <stdio.h>
#include "io.h"
#include "func.h"
#include <string.h>
//loads the book database
int load_saved_books() {
FILE *fp2 = fopen("saves.liborder", "r");
char checkerc[100];
int i = 0;
int fields = 0;
while (1) {
fgets(checkerc, 100, fp2);
remove_nl(checkerc);
//determining at which fields the chars are inserted
if (feof(fp2)) {
break;
}
if (i % 3 == 0) {
change_is_used(fields,1);
change_author(fields, checkerc);
//printf("auth: %s",get_book(fields).author);
}
if (i % 3 == 1) {
change_title(fields, checkerc);
//printf(gb.title);
}
if (i % 3 == 2) {
long long ibni = atol(checkerc);
change_ibn(fields,ibni);
if (get_head() == -1) {
change_head(fields);
change_tail(fields);
change_next (fields, -1);
}
else {
change_next(get_tail(), fields);
change_tail(fields);
change_next(fields, -1);
}
//printf("ibn: %lld\n",ibni);
fields++;
}
i++;
}
fclose(fp2);
return BOOK_OK;
}
int remove_nl(char* nl) {
char *p = nl;
int c = 0;
while (1) {
if (c > 98) {
break;
}
if (*(p+c) == '\n') {
*(p+c) = '\0';
return BOOK_OK;
}
c++;
}
return BOOK_ERROR;
}
int add_nl(char* nl) {
char *p = nl;
int c = 0;
while(1) {
if(*(p+c) == '\0') {
*(p+c) = '\n';
*(p+c+1) = '\0';
}
c++;
}
}
int *read_line(FILE *fp) {
int i = 0;
int *k = (int *)malloc(sizeof(int) * 100);
while (i < 100) {
int c = fgetc(fp);
if (c == '\n') {
return k;
}
else {
k[i] = c;
i++;
}
}
return k;
fclose(fp);
}
int number_of_lines(FILE *fp) {
int num = 0;
int i = 0;
while (i != EOF) {
i = fgetc(fp);
if (i == '\n')
num++;
}
return num;
}