-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreader.cpp
109 lines (81 loc) · 1.99 KB
/
reader.cpp
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
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX 64
#define MAXSTR 1000
#define CODHUFF 24
struct infos{
int simbolo;
char string[MAX];
int freq;
char codHuff[CODHUFF];
struct infos *prox;
}; typedef struct infos Tabela;
long getFileSize(FILE *file) {
long size;
fseek(file, 0, SEEK_END);
size = ftell(file);
fseek(file, 0, SEEK_SET);
return size;
}
void exibirTabela(char nomeArq[]){
FILE *tab = fopen(nomeArq,"rb");
Tabela tabela;
while(fread(&tabela,sizeof(Tabela),1,tab)==1){
printf("SIMBOL: %d\tSTR: %s\tfreq: %d\tcodHuff: %s\n",tabela.simbolo,tabela.string,tabela.freq,tabela.codHuff);
}
}
char* procurarNaTabela(char str[], FILE *tab){
rewind(tab);
Tabela tabela;
while(fread(&tabela,sizeof(Tabela),1,tab)==1){
if(strcmp(str,tabela.codHuff)==0){
return tabela.string;
}
}
return "";
}
void initStrings(char aux[], char palavraDecod[], char nomeArqTabela[]){
strcpy(aux,"");
strcpy(palavraDecod,"");
strcpy(nomeArqTabela,"");
}
void decodificar(char nomeArqCodigo[], char nomeArqTabela[]){
FILE *tab = fopen(nomeArqTabela,"rb");
FILE *cod = fopen(nomeArqCodigo,"r");
if (tab == NULL || cod == NULL) {
printf("Erro ao abrir arquivos!\n");
return;
}
int tamArq = getFileSize(cod);
char linha[tamArq],strDecod[MAXSTR];
int i=0;
Tabela tabela;
if(fgets(linha,tamArq,cod)){
char aux[MAXSTR],palavraDecod[MAXSTR],fraseDecod[MAXSTR];
initStrings(aux,palavraDecod,fraseDecod);
int cont = 0;
int achou = 0;
while(linha[i]!='\0'){
aux[cont] = linha[i];
aux[cont+1] = '\0';
strcpy(palavraDecod,procurarNaTabela(aux,tab));
if(strcmp(palavraDecod,"")!=0){
strcat(fraseDecod,palavraDecod);
cont = -1;
}
cont++;
i++;
}
printf("\n%s",fraseDecod);
}
fclose(tab);
fclose(cod);
}
int main(){
//FILE *cod = fopen("strCompilada.txt","r");
//printf("\nSize: %d",getFileSize(cod));
//fclose(cod);
exibirTabela("tabela.tabela");
decodificar("strCompilada.txt","tabela.tabela");
}