发布时间 : 星期三 文章用哈夫曼编码C语言实现文件压缩更新完毕开始阅读
ifp=fopen(filename,\ /*打开源文件*/ while(ifp==NULL) { printf(\打开文件出错\\n\ printf(\重新输入文件地址及文件名\ gets(filename);
ifp=fopen(filename,\ } printf(\输入解压后的文件地址和文件名及后缀:\ gets(wenjianming);
ofp=fopen(wenjianming,\ while(ofp==NULL) {
ofp=fopen(\解压的文件.txt\ }
fread(&flength,sizeof(long),1,ifp); fread(&f,sizeof(long),1,ifp); fseek(ifp,f,SEEK_SET); fread(&n,sizeof(long),1,ifp); for(i=0;i fread(&header[i].b,1,1,ifp); fread(&c,1,1,ifp); p=(long)c; header[i].count=p; header[i].bits[0]=0; if(p%8>0) m=p/8+1; else m=p/8; for(j=0;j fread(&c,1,1,ifp); f=c; itoa(f,buf,2); f=strlen(buf); for(l=8;l>f;l--) { strcat(header[i].bits,\ } strcat(header[i].bits,buf); } header[i].bits[p]=0; } for(i=0;i /*创建并打开目的文件*/ for(j=i+1;j if(strlen(header[i].bits)>strlen(header[j].bits)) { tmp=header[i]; header[i]=header[j]; header[j]=tmp; } } } p=strlen(header[n-1].bits); fseek(ifp,8,SEEK_SET); m=0; bx[0]=0; while(1) { while(strlen(bx)<(unsigned int)p) { fread(&c,1,1,ifp); f=c; itoa(f,buf,2); f=strlen(buf); for(l=8;l>f;l--) { strcat(bx,\ } strcat(bx,buf); } for(i=0;i if(memcmp(header[i].bits,bx,header[i].count)==0) break; } strcpy(bx,bx+header[i].count); c=header[i].b; fwrite(&c,1,1,ofp); m++; if(m==flength) break; } fclose(ifp); fclose(ofp); printf(\解压成功\\n\ return; } void main() /*主函数*/ {printf(\输入a开始解压\\n\printf(\输入b结束解压\\n\ while(1) { char c; c=getch(); if(c=='a') jieya(); else { if(c=='b') return;} } } 七、测试结果及分析: 压缩前的文件夹中的内容 压缩后的文件夹中的内容