0

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, &currentBit, &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.

ShadowGeist
  • 71
  • 1
  • 3
  • 9
  • Have you ever used a debugger? No? I recommend you get started with that right about now. – Mike Nakis Dec 19 '15 at 01:41
  • I have no idea how to use a debugger, so that's not a very good solution for me at the moment. – ShadowGeist Dec 19 '15 at 01:45
  • 1
    `fwrite()` returns a result that tells you how many items it wrote. You're quietly discarding that result. – Keith Thompson Dec 19 '15 at 01:50
  • @KeithT So, how would you suggest that i check whether `fwrite ( )` is writing or not? Should i assign the result of `fwrite ( )` to a variable and then check its result? – ShadowGeist Dec 19 '15 at 01:54
  • I already found out what the error was. Turns out the assignment `*bitCounter++` didn't properly increase `bitCounter` by 1, instead it did some random assignments that i don't quite understand. It was fixed by writing `*bitCounter = *bitCounter + 1` instead. Can't believe all this trouble was due to something so trivial. – ShadowGeist Dec 19 '15 at 02:04
  • There's a big difference between `*bitCounter++` (aka `*(bitCounter++)`) and `(*bitCounter)++`. Go and make sure you understand what the difference is, and hence why things went wrong. When things aren't working as you expect, if you don't know how to use debugger, make sure you know how to add statements to print crucial values — such as the values being compared in `if (*bitCounter == MAX_NUMBER_OF_BITS)`, so you know whether the `fwrite()` is even executed (it wasn't, was it?). Checking the initial value of `*bitCounter` would also be a good idea. – Jonathan Leffler Dec 19 '15 at 02:27
  • 1
    @JonathanLeffer I was checking why that operation went the way it did. If I'm not getting this wrong, it has to do something with the operator precedence. According [to this](http://en.cppreference.com/w/c/language/operator_precedence/), the operators _*_ and _++_ have the same precedence, and they are evaluated from right to left. So when i was doing this: `*bitCounter++`, instead of incrementing said value, i was setting its memory address one block further away, so when I asked for value it was showing me the value stored in said address. Was that the error i was committing more or less? – ShadowGeist Dec 19 '15 at 02:46
  • It is a very good idea to start to learn to use a debugger - one of the most useful tools for a programmer. – Ed Heal Jun 20 '17 at 12:24
  • I find it a good practice to be _paranoid_ (you know what word I'd rather use here) about parentheses. Also for getting started with `gdb` check out these resources: https://stackoverflow.com/questions/875205/gdb-cheat-sheet –  Jun 20 '17 at 12:43

1 Answers1

0

I was checking why that operation went the way it did. If I'm not getting this wrong, it has to do something with the operator precedence. According to this, the operators * and ++ have the same precedence, and they are evaluated from right to left. So when i was doing this: *bitCounter++, instead of incrementing said value, i was setting its memory address one block further away, so when I asked for value it was showing me the value stored in said address. Was that the error i was committing more or less? – ShadowGeist

That's almost right, except that *bitCounter++ yields the value stored at the address as it was before the increment, i. e. at the original bitCounter in the first loop cycle, at the next address in the second cycle, and so on.

Armali
  • 18,255
  • 14
  • 57
  • 171