Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C
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
Posted 7-May-13 12:14pm
Edited 7-May-13 16:14pm
v3

1 solution

Rate this: bad
good
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;
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);
}
  Permalink  

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

  Print Answers RSS
0 mhegazy94 488
1 Sergey Alexandrovich Kryukov 375
2 Kornfeld Eliyahu Peter 285
3 Sibeesh KV 251
4 Gihan Liyanage 189
0 Sergey Alexandrovich Kryukov 6,775
1 OriginalGriff 6,696
2 CPallini 5,345
3 George Jonsson 3,599
4 Gihan Liyanage 2,751


Advertise | Privacy | Mobile
Web01 | 2.8.140922.1 | Last Updated 8 May 2013
Copyright © CodeProject, 1999-2014
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