sao code của mình nén ra tỉ lệ cao hơn khi chưa nén nữa, các bạn xem dùm mình nha..mình nghĩ nó không đúng ở kiểu dữ liệu, phải dùng kiểu bit nhưng không biết sữa thế nào
Code:
#include "huff.h"
void main(int argc, char* argv[])
{
	char s[1000000] ="";
	char MaBit[100];
	FILE *pf;
	FILE *pff;
	int BangThongKe[255], j, k, count = 0, i = 0, n = 0, nBit = 0;
	bit TableBit[255];
	node dsNode[256];
	node *pRoot = NULL;
	
	if(strcmp(argv[1],"help") == 0)
	{
		printf("compression or uncompression the file\n");
		printf("huffman c|d i o\n");
		printf("   c: compression file\n");
		printf("   d: decompression file\n");
		printf("   i: path to input file\n");
		printf("   o: path to output file\n");
	}
	else
	{
		pf = fopen(argv[2],"r");
		pff = fopen(argv[3],"wb");
		while(!feof(pf))
		{
			s[n++] = getc(pf);
		}
		s[n-1] = NULL;
		fclose(pf);

		//buoc 1: lap bang thong ke
		ThongKe(BangThongKe,s);

		//buoc 2: tao cay huffman
		pRoot = &TaoCay(BangThongKe,dsNode,count);

		//buoc 3: phat sinh ma
			HuffCode(pRoot,TableBit,MaBit,i,nBit);

		//buoc 4: ma hoa file
		MaHoa(s,TableBit,pff);

		//buoc 5: giai ma
		//pff = fopen(argv[3],"rb");
		//GiaiMa(pRoot,pff);
		
	}
}
void GiaiMa(node *pRoot, FILE *pff)
{
	char c;
	node *pTemp = pRoot;
	while(!feof(pff))
	{
		c = fgetc(pff);
		if(c == '0')
			pRoot = pRoot->pLeft;
		if(c == '1')
			pRoot = pRoot->pRight;
		if(pRoot->pLeft == NULL && pRoot->pRight == NULL)
		{
			printf("%c",pRoot->c);
			pRoot = pTemp;
		}			
	}
}
void MaHoa(char s[],bit TableBit[], FILE *pf)
{
	int i, j, k;
	if(pf == NULL)
	{
			printf("Khong mo duoc file");
			return;
	}
	for(i = 0; i < strlen(s); i++)
	{
		for(j = 0; j < 255; j++)
			if(s[i] == TableBit[j].kitu)
			{
				for(k = 0; k < TableBit[j].sobit; k++)
					fputc(TableBit[j].s[k],pf);
			}
	}
	fclose(pf);
}
void HuffCode(node *pRoot, bit TableBit[255], char MaBit[], int &i, int &nBit)
{
	if(pRoot ==  NULL)
		return;
	if(pRoot->pLeft == NULL && pRoot->pRight == NULL)
	{
		TableBit[i].kitu = pRoot->c;
		TableBit[i].sobit = nBit;
		TableBit[i].s = new char[nBit];
		for(int j = 0; j < nBit; j ++)
			TableBit[i].s[j] = MaBit[j];
		i++;
		return;			
	}
	MaBit[nBit++] = '0';
	HuffCode(pRoot->pLeft,TableBit,MaBit,i,nBit);
	nBit--;
	MaBit[nBit++] = '1';
	HuffCode(pRoot->pRight,TableBit,MaBit,i,nBit);
	nBit--;
}
void ThongKe(int BangThongKe[], char s[])
{
	int i = 0;;
	while(i < 255)
	{
		BangThongKe[i] = 0;
		i++;
	}
	i = 0;
	while(i < strlen(s))
	{
		BangThongKe[(int)s[i]]++;
		i++;
	}
}
node TaoCay(int BangThongKe[], node dsNode[], int &count)
{
	int i,j;
	for( i = 1; i < 255; i++)
		if(BangThongKe[i] != 0)
		{
			dsNode[count].c = (char)i;
			dsNode[count].tanxuat = BangThongKe[i];
			dsNode[count].pLeft = NULL;
			dsNode[count].pRight = NULL;
			count++;
		}
	int count2 = count;
	TaoHeap(dsNode,count2-1);
	node huffNode[255*2-1];
	node m,n,newNode;
	j = 0;
	while(count2 > 1)
	{
		m = dsNode[0];
		dsNode[0] = dsNode[count2-1];
		count2--;
		TaoHeap(dsNode,count2-1);
		n = dsNode[0];
		huffNode[j++] = m;
		newNode.c = '\0';
		newNode.tanxuat = n.tanxuat + m.tanxuat;
		newNode.pLeft = &huffNode[j-1];
		huffNode[j++] = n;
		newNode.pRight = &huffNode[j-1];
		dsNode[0] = newNode;
		TaoHeap(dsNode,count2-1);
	}
	return dsNode[0];
}
void TaoHeap(node dsNode[], int count)
{
	int i = count/2;
	while(i >= 0)
	{
		HieuChinh(dsNode,i,count);
		i--;
	}
}
void HieuChinh(node dsNode[], int l, int r)
{
	int i = l;
	int j = 2*i+1;
	node x = dsNode[i];
	while(j <= r)
	{
		if(j + 1 <= r)
			if(dsNode[j].tanxuat > dsNode[j+1].tanxuat)
				j++;
		if(x.tanxuat < dsNode[j].tanxuat)
			return;
		else
		{
			dsNode[i] = dsNode[j];
			dsNode[j] = x;
			i = j;
			j = 2*i+1;
			x = dsNode[i];
		}
	}
}
x