Click here to Skip to main content
12,750,710 members (36,823 online)
Rate this:
Please Sign up or sign in to vote.
See more: C

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("Decoded text: ");
	decode(buf, q[1]);
	return 0;

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:

Decoded text:
Hello Code Project
Posted 7-May-13 13:14pm
Updated 7-May-13 17:14pm

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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;
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));
    // 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");
        // read file content into buffer
        fread(buf, 1, st.st_size, f);
        // append null byte
        buf[st.st_size] = 0;
    // close file
if (NULL != buf)
    // do your encoding here
    // ...
    // finally free the buffer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

    Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170215.1 | Last Updated 8 May 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100