如何为霍夫曼编码输入文件(使用C语言) [英] How to input a file for Huffman coding(using C language)

查看:93
本文介绍了如何为霍夫曼编码输入文件(使用C语言)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我写过一个编码和解码硬编码输入的Huffman C程序。现在我希望程序接受来自INPUT文件的文本而不是文件中的硬编码文本,然后将其传递给main中的编码函数,然后在我的霍夫曼树和频率构建之后进行解码。



我在最近4个小时内搜索了示例并尝试但是Xcode(IDE)在执行fgets函数时发出错误永远无法找到文件或给出(lldb)错误。我必须说它非常令人沮丧,但我想解决它。



一个例子或解决方案会很好。



这是我现有的代码,它接受有效的硬编码文本和输出。 huffmanpq.h是我使用霍夫曼和优先级队列数据结构的地方。



感谢您的帮助!

Hi,

I have written a Huffman C program that encodes and decodes a hardcoded input. Now I want to have the program accept text from an INPUT FILE instead of having hardcoded text in the file, which will then be passed to the encode function in main and then decoded, after the my huffman tree and frequencies are built.

I have searched the last 4 hours looking at examples and trying but Xcode(IDE) gives error can never locate the file or gives me (lldb) error when it executes the fgets function. I must say it is very frustrating, but I want to solve it.

An example or solution would be nice.

Here is the current code I have that accepts the hardcoded text that works and the output. The huffmanpq.h is where I put the huffman and priority queue data structures being used.

Thank you for any help!

#include <stdio.h>
#include <string.h>
#include "huffmanpq.h"

int main(void)

{
	int i;
	char *str = "Hello Code Project", buf[1024];  //given text to be encoded
	init(str); //creating huffman tree
	
	printf("The Huffman tree is frequency table is created.\n");
	for (i = 0; i < 128; i++)
		if (code[i]) printf("'%c': %s\n", i, code[i]); //display the huffman tree
    
	encode(str, buf);
	printf("\nEncoded text: %s\n", buf);
	printf("\n");
	printf("Decoded text: ");
	decode(buf, q[1]);
    
	return 0;
}



输出:

创建了霍夫曼树。


output:
The Huffman tree is created.

' ': 011
'C': 10110
'H': 0000
'P': 10111
'c': 0011
'd': 0001
'e': 100
'j': 0010
'l': 110
'o': 111
'r': 010
't': 1010



编码文本:


Encoded text:

000010011011011101110110111000110001110111010111001010000111010



解码文本:


Decoded text:

Hello Code Project

推荐答案

当你发布代码来阅读文件内容时会更好。然后我们就可以向您展示问题所在。



但是,将文本文件的内容读入缓冲区是一项基本任务:

It would be better when you have posted the code to read the file content. Then we could have shown you where the problems are.

However, reading the contents of a text file into a buffer is a basic task:
// file name; fixed name here as example
char *FileName = "/home/myaccount/test.txt";

char *buf = NULL;
FILE *f = NULL;
struct stat st;

// open file for reading
f = fopen(FileName, "r");
if (NULL == f)
    printf("Failed to open file %s: %s\n", FileName, strerror(errno));
else
{
    // get file info (we need the size)
    fstat(fileno(f), &st);
    // allocate buffer for file content including a byte for the NULL terminator
    buf = (char *)malloc(st.st_size + 1);
    if (NULL == buf)
        printf("Out of memory\n");
    else
    {
        // read file content into buffer
        fread(buf, 1, st.st_size, f);
        // append null byte
        buf[st.st_size] = 0;
    }
    // close file
    fclose(f);
}
if (NULL != buf)
{
    // do your encoding here
    // ...
    // finally free the buffer
    free(buf);
}


这篇关于如何为霍夫曼编码输入文件(使用C语言)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆