void HuffmanEncode( FILE *fp, BITFILE *bf)
{
unsigned long filelength;
int i;
BuildCounts( fp);
fseek( fp, 0, SEEK_END);
filelength = ftell( fp);
BitsOutput( bf, filelength, 4*8);
for( i=0; i<256; i++) BitsOutput( bf, counts[i], 4*8);
BuildTree( );
BuildCode( );
fseek( fp, 0, SEEK_SET);
while( EOF!=(i=fgetc(fp)))
{
BitsOutput( bf, hCode[i].code, hCode[i].codelength);
}
}
void HuffmanDecode( BITFILE *bf, FILE *fp)
{
unsigned long filelength;
int i;
int rootNode;
int node;
filelength = ftell( fp);
if( -1 == filelength) filelength = 0;
// reading the counts
for( i=0; i<256; i++) counts[i] = BitsInput( bf, 4*8);
rootNode = BuildTree( );
while( 0<filelength)
{
node = rootNode;
do{
if( 0 == BitInput( bf)) node = hTree[node].lchild;
else node = hTree[node].rchild;
}while( 255<node);
fputc( node, fp);
filelength --;
}
}
void CMy111Dlg::OnCode()
{
if(m_Path == "")
{
AfxMessageBox("请选择路径");
return;
}
//译码,编码
char *ch = m_Filename.GetBuffer(0);
FILE *fp;
BITFILE *bf;
CString str = "";
CString str1 = "";
this->m_Style.GetWindowText(str1);
this->m_Code.GetWindowText(str);
if( str == "编码")
{ // do encoding
fp = fopen( m_Filename, "rb");
bf = OpenBitFileOutput( "result.txt");
if( NULL!=fp && NULL!=bf)
{
if( str1 == "huffman")
fclose( fp);
CloseBitFileOutput( bf);
}
}
else
if( str == "译码")
{ // do decoding
bf = OpenBitFileInput( "_result.txt");
fp = fopen( m_Filename, "r");
if( NULL!=fp && NULL!=bf)
{
if( str1 == "huffman")
HuffmanDecode(bf,fp);
fclose( fp);
CloseBitFileInput( bf);
}
}
对一文本文件编码,能够成功,但对编码后的结果译码,出不了结果,老是生成空文件,大家帮忙帮我看看!!!