C++ Help, Huffman Decoding

Discussion in 'OT Technology' started by SPACECATAZ, Mar 26, 2009.

  1. SPACECATAZ

    SPACECATAZ New Member

    Joined:
    Dec 22, 2006
    Messages:
    2,502
    Likes Received:
    0
    Hey guys, I can't seem to find the cause for my program constantly crashing. I'm trying to use a reconstructed huffman tree that I've read back into the program from all binary numbers. I'm now at the point where I'm trying to print back out the characters on the tree into the decoded file, but it keeps crashing when I run it, even though it compiles right.

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <cstdio>
    #include "Huffman.h"
    #include "binary2.h"
    #define EOF (-1)
    using namespace std;
    
    /****************************************************************
     *                   huffReadTree                               *
     ****************************************************************
     * huffReadTree() reads an encoded file of binary numbers and    *
     *  builds a suitable tree, in a form that can be written by    *
     *  huffDecode.                                                  *
     ****************************************************************/
    
    Node* huffReadTree(BFILE* inFile)
    {
        int bit = readBit(inFile);
        if(bit == 1)
        {
            int c = readByte(inFile);
            return new Node(c);
        }
        else
        {
            Node* L = huffReadTree(inFile);
            Node* R = huffReadTree(inFile);
            return new Node(L,R);
        }   
    }
    
    /****************************************************************
     *                   huffDecode                                 *
     ****************************************************************
     * huffDecode() writes tree t's node characters out in order    *
     *   according to their placement in the tree.                   *
     ****************************************************************/
    
    void huffDecode(Node* t, BFILE* inFile, char* filename)
    {
        Node* treeF = t; 
        ofstream out(filename);
        while(true)
        { 
            int c = readBit(inFile);
            if(c == EOF) break;
            if(c == 0)
            {
                treeF = treeF->left;
            }
            else
            {    
                treeF = treeF->right;
            }
            if(treeF == treeF->left)       
            {
                out << treeF->ch;
                treeF = t;
            }
            else if(treeF == treeF->right)
            {
                out << treeF->ch;
                treeF = t;
            }
        }
    }
      
    
         
    int main(int argc, char** argv)
    {
            BFILE* inFile = openBinaryFileRead(argv[1]);
            Node* t = huffReadTree(inFile);
            huffDecode(t, inFile, argv[2]);
    }
            
    
    Am I searching the tree wrong? :wtc:

    argv[1] = encoded file
    argv[2] = output file
     
  2. skinjob

    skinjob Active Member

    Joined:
    Jan 6, 2001
    Messages:
    2,337
    Likes Received:
    0
    Location:
    Aztlán
    I'm assuming that if a node has no left or right child, its left or right child pointer will be NULL. If that's the case, your search function never checks whether a child pointer is valid before it tries to access that node's members.
     

Share This Page