-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomprimir.cpp
45 lines (36 loc) · 1006 Bytes
/
comprimir.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
#include "bib.h"
// Comprime arquivo fonte passado, armazenando os valores de frequencia e o arquivo comprimido
bool comprimir(string arqFonte, string arqFreq, string arqComp) {
// Abre arquivos
ifstream fonte(arqFonte, ifstream::binary);
ofstream freq(arqFreq), comp(arqComp, ofstream::binary);
// Variaveis locais
unsigned char atual;
int * vet = frequencia(fonte);
string * codigos = gerarCodigo(gerarArvore(vet));
fonte.close();
fonte.open(arqFonte);
// Le entrada
while(!fonte.eof()) {
fonte >> noskipws >> atual;
// Gambiarra
if (fonte.eof()) {
break;
}
// Escreve codigo relativo ao caractere atual na saida
if (atual == 32) {
comp << codigos[26];
}
else {
comp << codigos[atual - 65];
}
}
// Escreve frequencias em outro arquivo
for (int i = 0; i < 27; i++) {
freq << vet[i] << endl;
}
fonte.close();
comp.close();
freq.close();
return true;
}