www.pudn.com > huffman.rar > DeCode.c
#include "ECBTree.h" #include "MyAssert.h" #include "Ulti.h" #include#include #include #define LENGTH 128 #define DEBUG 1 char dotTxt[]=".txt"; char dotRer[]=".rer"; char cmpStr[]=".rer"; void main(int argc,char* argv[]) { //core data. long wList[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,nodeIndex,count; //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]); for(i=0;i<4;i++) cmpStr[i]=inputFileName[fileNameLen-4+i]; cmpStr[4]='\0'; if(strcmp(cmpStr,dotRer)!=0) { printf("file not recognized ,should be fileName.rer"); return; } //only could zip the file appendix as .***,*** have to be 3 char length. outputFileName[fileNameLen-4]='\0'; strcat(outputFileName,dotTxt); inputFileName[fileNameLen]='\0'; outputFileName[fileNameLen+4]='\0'; 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; while(!feof(inputFile)) { /*----------------------------*/ if(count==8) { //1.get data. inData=fgetc(inputFile);//get 8 len bin haff code. if(inData==-1&&feof(inputFile)) break; // curIndex=7; count=0; } /* when its the child node search a unuseful step. tmpBinData=(inData>>curIndex)&0x01; */ if(myHtTree->ht[nodeIndex].llinkIndex==-1&&myHtTree->ht[nodeIndex].rlinkIndex==-1) { //send out data. fputc(myHtTree->ht[nodeIndex].info,outputFile); nodeIndex=myHtTree->rootIndex; } else { tmpBinData=(inData>>(7-count))&0x01; if(tmpBinData==0x00) nodeIndex=myHtTree->ht[nodeIndex].llinkIndex; else if(tmpBinData==0x01) nodeIndex=myHtTree->ht[nodeIndex].rlinkIndex; else printf("error happen in read bin!\n"); // curIndex--; count++; } } if(DEBUG) printf("\ntest ends.\n"); //step3:end of code. fclose(inputFile); fclose(outputFile); getchar(); return; }