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

📄 file_dealing.h

📁 哈弗曼编码
💻 H
字号:
#include "stdio.h"
#include "iostream.h"
#include "huffman_tree.h"
#include "define.h"

bool put_into_ascii(NODE *,char *);
bool put_into_code(NODE *,char *);
bool fullfill_the_weight_array_and_create_weight_file(void);
bool fullfill_the_weight_array_by_reading_weight_file(char *);

bool fullfill_the_weight_array_by_reading_weight_file(void)
{
	int i=0;
	FILE *weight_file;
	if ((weight_file=fopen("weight.txt","rt"))==NULL)
	{
		cout<<"The weight file \"weight.txt\" doesn't exist !"<<endl;
		return false;
	}
	while(!feof(weight_file))
	{
		fread(&buffer[i],sizeof(BUFF),1,weight_file);
		if (buffer[i].freq==0 || buffer[i].freq==MAX)
			break;
		i++;
	}
	fclose(weight_file);
	cout<<"\n";
	return true;
}

bool fullfill_the_weight_array_and_create_weight_file(char *_file_to_compile)
{
	FILE *file_to_compile,*weight_file;
	int i;
	char letter;
	for (i=0;i<95;i++)
	{
		buffer[i].chrctr=-1;
		buffer[i].freq=0;
	}
	file_to_compile=fopen(_file_to_compile,"rt");
	if (file_to_compile==NULL)
	{
		cout<<"File doesn't exist,check your spelling !"<<endl;
		return false;
	}
	else
	{
		while(!feof(file_to_compile))
		{
			letter=fgetc(file_to_compile);
			if (letter==10 || (letter>=32 && letter<=126))
			{
				for (i=0;i<95;i++)
				{
					if (buffer[i].chrctr==letter)
					{
						buffer[i].freq++;
						break;
					}
					else if (buffer[i].chrctr==-1)
					{
						buffer[i].chrctr=letter;
						buffer[i].freq++;
						break;
					}
				}
			}
		}
		fclose(file_to_compile);
		i=0;	
		weight_file=fopen("weight.txt","wt");
		while(buffer[i].chrctr!=-1)
		{
			fwrite(&buffer[i],sizeof(BUFF),1,weight_file);
			i++;
		} 
		fclose(weight_file);
		cout<<"\nThe weight array was built !"<<endl;
	} 
	return true;
}

bool put_into_ascii(NODE *head,char *_file_to_translate)
{
	FILE *file_compiled,*file_ascii;
	NODE *p=head;
	char code;
	if (head==NULL)
	{
		cout<<"Error in the returned point of huffman tree !(function put_into_ascii)"<<endl;
		return false;
	}
	if ((file_compiled=fopen(_file_to_translate,"rt"))==NULL)
	{
		cout<<"error in opening compiled file !"<<endl;
		return false;
	}
	file_ascii=fopen("translated.txt","wt");
	while(!feof(file_compiled))
	{
		code=fgetc(file_compiled);
		if (code!='0' && code!='1')
		{
			fputc(code,file_ascii);
			continue;
		}
		else if(code=='1')
		{
			p=p->rc;
			if (p->lc==p->rc)
			{
				fputc(p->c,file_ascii);
				p=head;
			}
		}
		else if (code=='0')
		{
			p=p->lc;
			if (p->lc==p->rc)
			{
				fputc(p->c,file_ascii);
				p=head;
			}
		}
	}
	fclose(file_compiled);
	fclose(file_ascii);
	return true;
}

bool put_into_code(NODE *head,char *_file_to_compile)
{
	FILE *file_to_compile,*code_file;
	char c;
	code_file=fopen("code.txt","wt");
	file_to_compile=fopen(_file_to_compile,"rt");
	while(!feof(file_to_compile))
	{
		c=fgetc(file_to_compile);
		if (c==10 || (c>=32 && c<=126))
		{
			while(r!=-1)
				pop();
			found=false;
			compile_each_character(head,c);
			r=0;
			while(stack[r])
			{
				fputc(stack[r],code_file);
				r++;
			}
		}
		else
			fputc(c,code_file);
	}
	fclose(file_to_compile);
	fclose(code_file);
	return true;
}

⌨️ 快捷键说明

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