-
Notifications
You must be signed in to change notification settings - Fork 2
/
inputOutput.c
105 lines (78 loc) · 2.28 KB
/
inputOutput.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
//
// inputOutput.c
// ARQ_TP6
#include "inputOutput.h"
char *text;
long long int txt_len;
int nk; //numero de claves
long long int **results;
int *n_results; //controla cuantos resultados hay de cada tipo
void read_keys(char *path){
int fd;
int i = 0;
int j = 0;//cuenta total de caracteres
int lineas = 1;
int max_linesize = 1000; //un linesize infinito requiere recorrer 2 veces
char c;
keys = (char**) malloc(sizeof(char*)*lineas);
keys[lineas-1] = malloc(sizeof(char)*max_linesize);
if((fd = open(path, O_RDONLY)) == -1)
printf("error abriendo archivo claves\n");
while( read(fd, &c, 1) > 0){//-1 es error y 0 es EOF
if((c == '\n') || (c == '\r')){
*(keys[lineas-1]+i) = '\0';
lineas++;
if (i > longest_key)
longest_key = i;
i = 0;
keys = (char**) realloc(keys, sizeof(char*)*lineas);
keys[lineas-1] = malloc(sizeof(char)*max_linesize);
}else{
*(keys[lineas-1]+i) = c;
i++;
j++;
}
}
if(*keys[lineas-1] == '\0'){
//printf("eliminando retorno de carro extra de entrada\n");
free(keys[lineas-1]);
lineas--;
}
nk = lineas;
snk = j+1;
close(fd);
}
void read_text(char *path){
//lee texto y lo guarda en strin global text
int fd;
if( (fd = open(path, O_RDONLY)) == -1){
printf("error abriendo archivo text\n");
exit(1);
}
long long int size = lseek(fd, 0, SEEK_END);
text = malloc(size*sizeof(char));
lseek(fd, 0, SEEK_SET);
if( size != read(fd, text, size) )
printf("no se ha leido todo el texto\n");
close(fd);
}
void print_stdout(){
for(int u=0;u<nk;u++){
printf("%s\t\t%d\n",keys[u],n_results[u]);
}
}
void file_out(){
for(int u=0;u<nk;u++){
printf("%s\t\t",keys[u]);
printf("%d\t",n_results[u]);
for(int j=0;j<(n_results[u]);j++){
printf("%llu\t",results[u][j]);
}
printf("\n");
}
}
int cmpfunc (const void * a, const void * b){
int va = *(const int*) a;
int vb = *(const int*) b;
return (va > vb) - (va < vb);
}