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]; init(str);
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]);
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