Reputation: 21
The problem is that I can decode a data stream for the first time, but then it will become an infinite loop and display the same value over and over again... I'm using borland C++. Decoding is done by first saving the text a to z in an array, then taking the input data stream and then cutting the contents of the array using strcpy then comparing with the contents of the first array then if a match is found, the corresponding ASCII is printed.
code:
#include<conio.h>
#include<iostream.h>
#include<string.h>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
int codes[512];
char cut_stream[100];
char input_stream[100];
char decoded_stream[256];
int test,count,cut_start,cut_end,length_of_stream,final,temp_loop_s,temp_loop_e,temp_num,comp_num,send;
void select_num(int select)
{
int output;
output = select + 64;
cout << char(output);
}
void decode()
{
cout<<"\nEnter the data stream ";//<<endl;
cin >> input_stream;
length_of_stream = strlen(input_stream);
//cout<< length_of_stream ; //only for debugging
/********************************************
the code starts here...
********************************************/
count = length_of_stream;
//count -= count;
for(int ini =0; ini<=count ; ini++)
{
for( final=1;final<=count;final++)
{
strncpy(cut_stream, &input_stream[ini], final);
//cut_start = cut_start + 1;
/*********************************************
compare
*********************************************/
temp_num = atoi(cut_stream);
for(int z= 1;z<=26;z++)
{
comp_num = codes[z];
if(comp_num == temp_num)
{
send =z;
select_num(send);
test =1;
comp_num =0;
break;
}
}
if( test ==1)
{
test =0;
ini = final-1; // the increment will be given on the next for loop so it is reduced here
//final =0;
//cout<< "im in test";
break;
}
}
}
cout<< "end";
while(1);
}
//cout<< decoded_stream;
//while(1);
void main()
{
cut_start =0;
cut_end = 1;
cout << "Huffman decoder" << endl;
cout << "Enter the codes for a-z" << endl;
for(int i =1;i<=3;i++)
{
cin >> codes[i];
}
decode();
}
Upvotes: 2
Views: 447
Reputation: 25522
There are least two major bugs in the code:
The codes[] array is uninitialized mostly, you only read in three numbers even though you later access the array for indexes up to 26 or so.
The call to strncpy() is broken in the sense that strncpy() does NOT null-terminate a string when it copies the maximum number of characters; that is, when you call strncpy() with final set to 1, strncpy() copies one character and does NOT append the terminating NUL-character, which will then cause atoi() to fail.
Also, if you are using "0" and "1" characters in your huffman coding, this won't work anyway because numbers "01" and "1" will both be interpreted by atoi() as 1 (one) even though they are different codes. If this is really huffman-coding you shouldn't be using atoi() and integers at all, but just binary or character strings.
Huffman decoding is better done by using a tree data structure. look up any standard book on algorithms for reference.
Upvotes: 2