I'm currently writing a Huffman code to compress and decompress a file. I'm currently at the stage of converting the "routes" of each character in the Huffman tree into an int and writing it into a binary file. This is what I've got:
int ConversionToBinary ( FILE *binaryFile, char *string, int *currentBit, int *bitCounter ) {
int x;
int stringLength;
stringLength = CalculateStringLength ( string );
short int i;
for ( i = 0; i < stringLength; i++ ) {
x = ConversionToNumber ( string [ i ] );
*currentBit = x | 1 << *contadorDeBits;
*bitCounter++;
if ( *bitCounter == MAX_NUMBER_OF_BITS ) {
fwrite ( currentBit, 1, sizeof ( int ), binaryFile );
*currentBit = 0;
*bitCounter = 0;
}
}
}
That section of code was based on what i found in this thread:
This is the relevant part of the function that is calling ConversionToBinary, just in case:
currentBit = bitCounter = 0;
EmptyString ( string );
while ( ( character = fgetc ( textFile ) ) != EOF ) {
auxElement = SearchInHashTable ( HashTable, character );
strcpy ( string, auxElement -> route );
ConversionToBinary ( binaryFile, string, ¤tBit, &bitCounter );
EmptyString ( string );
}
As you could see, I stored all the relevant data in a Hash Table so I could easily retrieve it from there.
Thing is, when I call ConversionToBinary, it doesn't write anything, and I don't know why that is. I've checked if the condition *( bitCounter == MAX_NUMBER_OF_BITS ) was ever met, but logically it should, so even if the actual conversion was wrong, at least it should write something in my file, but it doesn't.
I'm currently out of ideas already, i need a bit of help.