-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhuff.c
More file actions
54 lines (54 loc) · 943 Bytes
/
huff.c
File metadata and controls
54 lines (54 loc) · 943 Bytes
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
#include<stdio.h>
#include<math.h>
void main()
{
FILE *fp,*fw ;
int i=0;
char ch;
int s=0;
int decNum=0;
int rem=0;
int f=0;
fp = fopen("samorig.txt", "r") ;
fw = fopen("compressed_huff.txt", "w") ;
if ( fp == NULL)
{
printf( "File failed to open." ) ;
}
else
{
fseek(fp, -3, SEEK_END);
s=ftell(fp);
}
while(s>=0)
{
ch=fgetc(fp);
rem=ch - '0';
f = decNum;
decNum += rem*pow(2,i);
i++;
if (i==7)
{
decNum=decNum+33;
printf("%d:",decNum);
printf("%c\n",decNum);
fprintf(fw, "%c",decNum);
i=0;
decNum=0;
}
else
{
if (s==0)
{
decNum=decNum+33;
printf("%d:",decNum);
printf("%c",decNum);
fprintf(fw, "%c",decNum);
}
}
fseek(fp, -2, SEEK_CUR);
s--;
}
fclose(fp);
fclose(fw);
}