-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
executable file
·95 lines (70 loc) · 1.99 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "BF.h"
#include "Sort.h"
#include "Heap.h"
#define fileName "heapFile"
void insert_Entries(int);
int main(int argc, char **argv) {
int fd;
int fieldNo = 0;
BF_Init();
//create heap file
if (Sorted_CreateFile(fileName) == -1)
printf("Error creating file in main!\n");
fd = Sorted_OpenFile(fileName);
if (fd == -1)
printf("Error opening file in main!\n");
insert_Entries(fd);
if (Sorted_CloseFile(fd) < 0) {
printf("Error closing file in main!\n");
}
//sort heap file using 2-way merge-sort
Sorted_SortFile(fileName, 0);
if (Sorted_checkSortedFile("heapFileSorted0", fieldNo) < 0) {
printf("Not sorted!\n");
}
else {
printf("sorted!\n");
}
//get all entries with value
//char value[20];
//strcpy(value, "Keratsini");
//int value = 11903588;
fd = Sorted_OpenFile("heapFileSorted0");
if( fd == -1 )
printf("Error opening file!\n");
printf("\n\n\n");
Sorted_GetAllEntries(fd, &fieldNo, NULL);
return EXIT_SUCCESS;
}
void insert_Entries(int fd) {
FILE *stream;
char *line = NULL;
size_t len = 0;
ssize_t read;
stream = stdin;
Record record;
while ((read = getline(&line, &len, stream)) != -1) {
line[read - 2] = 0;
char *pch;
pch = strtok(line, ",");
record.id = atoi(pch);
pch = strtok(NULL, ",");
pch++;
pch[strlen(pch) - 1] = 0;
strncpy(record.name, pch, sizeof(record.name));
pch = strtok(NULL, ",");
pch++;
pch[strlen(pch) - 1] = 0;
strncpy(record.surname, pch, sizeof(record.surname));
pch = strtok(NULL, ",");
pch++;
pch[strlen(pch) - 1] = 0;
strncpy(record.city, pch, sizeof(record.city));
assert(!Sorted_InsertEntry(fd, record));
}
free(line);
}