T
togipete
I'm working on a huffman encoding program right now for my C class - I
got the frequency and encoding table working just fine but am having
problems writing the output. The output format is: int representing the
number of chars in the freq table (4 bytes), followed by the first char
in the freq table (1 byte) followed by its count (int, 4 bytes) for
each element in the freq table. File output is supposed to be done
using unbuffered IO
char *tchar; /* buffer to hold totalchars as a string to write */
int totalchars;
....
tchar = (char *)malloc( sizeof(int) );
sprintf(tchar, "%d", totalchars);
write(outfd,tchar,sizeof(int));
This is just for the first part of the file with the total number of
chars but the problem seems to be the same in the rest of the output...
say my totalchars was 5, when I view my encoded file im expecting to
see a 05 00 00 00 to start the file, but I get some random garbage like
35 00 1a c0 (I'm still learning the language, but it looks as though
the rest of string has old garbage in it still). What would be the best
way to initialize the string to 0s or what would be a better solution?
got the frequency and encoding table working just fine but am having
problems writing the output. The output format is: int representing the
number of chars in the freq table (4 bytes), followed by the first char
in the freq table (1 byte) followed by its count (int, 4 bytes) for
each element in the freq table. File output is supposed to be done
using unbuffered IO
char *tchar; /* buffer to hold totalchars as a string to write */
int totalchars;
....
tchar = (char *)malloc( sizeof(int) );
sprintf(tchar, "%d", totalchars);
write(outfd,tchar,sizeof(int));
This is just for the first part of the file with the total number of
chars but the problem seems to be the same in the rest of the output...
say my totalchars was 5, when I view my encoded file im expecting to
see a 05 00 00 00 to start the file, but I get some random garbage like
35 00 1a c0 (I'm still learning the language, but it looks as though
the rest of string has old garbage in it still). What would be the best
way to initialize the string to 0s or what would be a better solution?