⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 4.cpp

📁 huffman 编码与译码 对任意文件二进制的编码与译码
💻 CPP
字号:
#include"stdio.h"
#include"stdlib.h"
#include"string.h"
#include"math.h"
struct node
{unsigned char data;
 char *c;
};
void main()
{ struct node *s;
  int i,n,l;
  unsigned char x;
  char str[100],filename[20],file2[20],w[100],ch;
  FILE *fp1,*fp2,*fp3,*fp4;
   printf("请输入文件名:");
 gets(filename);
fp1=fopen(filename,"rb");
if(fp1==NULL)
 {printf("对不起!该文件不存在。");
 exit(0);
 }
fp4=fopen("out1.txt","r");
 if(fp4==NULL)
  {printf("概率文件不存在");
  exit(0);
  }
  fscanf(fp4,"%d",&n); 
  fclose(fp4);
  fp2=fopen("temp.txt","r");
  if(fp2==NULL)
  {printf("字典文件不存在");
  exit(0);
  }
 
  s=(struct node *)calloc(n,sizeof(struct node));
  fscanf(fp2,"%d%s",&x,str);
  i=0;
 while(!feof(fp2))
  {l=strlen(str);
   s[i].c=(char *)calloc(l+1,sizeof(char));
   s[i].data=x;
   strcpy(s[i].c,str);
   fscanf(fp2,"%d%s",&x,str);
   i++;
  }
  fclose(fp2);
  fp3=fopen("temp.dat","wb");
  str[0]=0;w[1]=0;
  ch=fgetc(fp1);
while(!feof(fp1))
 {  w[0]=ch;
    strcat(str,w);   
	for(i=0;i<n;i++)
       if(strcmp(str,s[i].c)==0)
       break;
	   if(i!=n)
       {fputc(s[i].data,fp3);
	   str[0]=0;
	   }
        ch=fgetc(fp1);
 }
 fclose(fp1);
 fclose(fp3);
 remove(filename);
 remove(file2);
 rename("temp.dat",filename);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -