-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
146 lines (110 loc) · 2.59 KB
/
main.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include <stdio.h>
#include <stdlib.h>
#define FILE_NAME "database.db"
typedef struct Data
{
int value;
int id;
} Data;
void addToDatabase(Data *data, FILE *fptr);
void deleteFromDatabase(int id, FILE *fptr);
void printData(FILE *fptr);
int getTotalItems(FILE *fptr);
int main(void)
{
// Variables
FILE *fptr;
Data *newData1, *newData2, *newData3;
// Create new data
newData1 = malloc(sizeof(Data) * 1);
newData1->value = 10;
newData1->id = 1;
newData2 = malloc(sizeof(Data) * 1);
newData2->value = 20;
newData2->id = 2;
newData3 = malloc(sizeof(Data) * 1);
newData3->value = 30;
newData3->id = 3;
// Open the file
fptr = fopen(FILE_NAME, "w+");
// Manipulate the database
addToDatabase(newData1, fptr);
addToDatabase(newData2, fptr);
addToDatabase(newData3, fptr);
deleteFromDatabase(1, fptr);
printData(fptr);
// Close the file
fclose(fptr);
// Free data
free(newData1);
free(newData2);
free(newData3);
return 0;
}
int getTotalItems(FILE *fptr)
{
int totalItems;
fseek(fptr, 0, SEEK_END);
totalItems = ftell(fptr) / sizeof(Data);
fseek(fptr, 0, SEEK_SET);
return totalItems;
}
void addToDatabase(Data *data, FILE *fptr)
{
fwrite(data, sizeof(Data), 1, fptr);
}
void deleteFromDatabase(int id, FILE *fptr)
{
Data *data, *newData = NULL;
int totalItems, i, found = 0, j = 0;
// Get the total number of items in the database
totalItems = getTotalItems(fptr);
if (totalItems == 0)
{
return;
}
// allocate memory for the data
data = malloc(sizeof(Data) * totalItems);
fread(data, sizeof(Data), totalItems, fptr);
for (i = 0; i < totalItems; i++)
{
if (data[i].id == id)
{
found = 1;
continue;
}
newData = realloc(newData, sizeof(Data) * ++j);
newData[j - 1] = data[i];
}
if (found == 1)
{
// Re-open the file in write mode to clear existing data
fptr = freopen(FILE_NAME, "w", fptr);
// Write the new data
fwrite(newData, sizeof(Data), totalItems - 1, fptr);
}
free(data);
free(newData);
}
void printData(FILE *fptr)
{
Data *data = NULL;
int totalItems, i;
// Re-open the file in read mode
fptr = freopen(FILE_NAME, "r+", fptr);
// Get the total number of items in the database
totalItems = getTotalItems(fptr);
if (totalItems == 0)
{
printf("There are no items in the database.\n");
return;
}
data = malloc(sizeof(data) * totalItems);
fread(data, sizeof(Data), totalItems, fptr);
for (i = 0; i < totalItems; i++)
{
printf("[DATA]\nID: %d\nValue: %d\n\n", data[i].id, data[i].value);
}
// Free memory
free(data);
}