www.pudn.com > huffman.rar > Code.c
#include "ECBTree.h" #include "MyAssert.h" #include "Ulti.h" #include#include #include #define LENGTH 128 #define DEBUG 1 #define REARPOS 80 char dotTxt[]=".txt"; char dotRer[]=".rer"; int getBinLen(unsigned long inData); void main(int argc,char* argv[]) { //core data. long wList[LENGTH]; unsigned long haffCodeList[LENGTH]; int haffCodeLen[LENGTH]; HaffCode haffList[LENGTH]; PHtTree myHtTree; //file data. char inputFileName[LENGTH],outputFileName[LENGTH]; FILE* inputFile,* outputFile,* keyFile; int fileNameLen; //binary opeartion data. char inData,outputData; unsigned long curCode,tmpBinData; int curLen,realLen,curIndex; int i; int count; unsigned long rearCode;/*rear data consult*/ int rearCodeLen; //open the files. if (argc<=1) { printf("please enter your file address.\n"); return; } else { //make up the files' name. strcpy(inputFileName,argv[1]); strcpy(outputFileName,argv[1]); fileNameLen=strlen(argv[1]); //only could zip the file appendix as .***,*** have to be 3 char length. outputFileName[fileNameLen-4]='\0'; strcat(outputFileName,dotRer); //strcat(inputFileName,dotTxt); inputFileName[fileNameLen]='\0'; outputFileName[fileNameLen+4]='\0'; /*inputFile open.*/ if((inputFile=fopen(inputFileName,"rb"))==NULL) { printf("file path not found\n"); return; } if (DEBUG) printf("input file open success\n"); /*outpout file open*/ assertF((outputFile=fopen(outputFileName,"wb"))!=NULL,"output file error"); if (DEBUG) printf("output file open success\n"); } if((keyFile=fopen("KEY.txt","rb"))==NULL) { printf(">--keyFile not founded--<\n"); return; } for(i=0;i rootIndex); //step2: for(i=0;i rootIndex,0x000000,0,haffList); fprintf(stdout,"haffCode List:\r\n"); for(i=0;i >(rearCodeLen-1-i))&0x01); } } /* the consult below will make error happen! outputData<<=(8-count); */ break; } //2.search table ->Should be a ascii file. curCode=haffList[inData].haffCode; curLen=haffList[inData].haffCodeLen; realLen=getBinLen(curCode); i=curLen-realLen; curIndex=0; } if(i>0) { outputData<<=1; //no need to fill bit data. i--; } else { tmpBinData=(curCode>>(curLen-curIndex-1))&0x01; outputData<<=1; outputData|=(char)tmpBinData; } /*-----------------------------------*/ curIndex++; count++; } fputc(outputData,outputFile); } if(DEBUG) printf("\ntest ends.\n"); //step3:end of code. fclose(inputFile); fclose(outputFile); getchar(); return; } int getBinLen(unsigned long inData) { int i=0; if( inData==0) return 1; else while(inData!=0) { inData/=2; i++; } return i; }