-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhuffmanencoding.cpp
96 lines (75 loc) · 1.57 KB
/
huffmanencoding.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
#include<bits/stdc++.h>
#include "bitChar.h"
#include "HuffmanTree.h"
using namespace std;
#define ul unsigned long
string text;
ul f[257];
char fpath[200] ,nfile[200] , newfile[200]={'E',':'} ,freqfile[200];
ul getsize(char *filename)
{
streampos begin,end;
ifstream myfile (filename, ios::binary);
begin = myfile.tellg();
myfile.seekg (0, ios::end);
end = myfile.tellg();
myfile.close();
return end-begin;
}
void filesave()
{
int i;
for(i=strlen(fpath)-1;i>=0;i--)
{
if(fpath[i]==92) break;
}
int k=0;
for(int j=i;fpath[j]!='.';j++)
nfile[k++]=fpath[j];
nfile[k]='\0';
strcat(newfile,nfile);
strcpy(freqfile,newfile);
strcat(newfile,".dat");
strcat(freqfile,".freq");
ofstream of1(newfile) , of2(freqfile);
bitChar bchar;
for(int j=0;j<257;j++)
{
if(freqof(j))
of2<<j<<" "<<freqof(j)<<endl;
}
of2.close();
string encoded="";
for(ul j=0;j<text.length();j++)
{
int x=text[j];
encoded+=codeof(text[j]);
}
bchar.setBITS(encoded);
bchar.insertBits(of1);
of1.close();
}
int main()
{
printf("Enter File path to compress:");
cin>>fpath;
clock_t tStart = clock();
ifstream ifs(fpath);
char ch;
text="";
while(!ifs.eof())
{
ifs.get(ch);
f[ch]++;
text+=ch;
}
ifs.close();
populatepq(f);
cout<<"Compressing file....."<<endl;
buildHuffmanTree();
treetraversal(root,0);
filesave();
cout<<"Original size..."<<getsize(fpath)<<" bytes."<<endl;
cout<<"Size after compression..."<<getsize(newfile)<<" bytes."<<endl;
printf("Time taken to compress: %.2fs\n", (double)(clock() - tStart)/CLOCKS_PER_SEC);
}