| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Issue implementing algorithm of Lempel Ziv
I have been struggling with this program for many hours. I've also looked through
most all of the posts listed here for c++ relevant to my issue. The problem is with my array that holds code word not seen yet, it gets replaced everytime, let me explain. I have my dictionary of code words setup as a struct with two variables "code" and "key". I have no problem creating a dictionary and I can even add to it. What happens is I do this: STRING = get input character WHILE there are still input characters CHARACTER = get input character IF STRING+CHARACTER is in the string table then STRING = STRING+character ELSE output the code for STRING add STRING+CHARACTER to the string table STRING = CHARACTER END of IF END of WHILE Everything works great the first time through the loop. Then what happens is not good. The buffer I use to hold the STRING+CHARACTER writes itself into all the following new structs I create that hold more code words for the dictionary. Unfortunately my code is not very clear, but I seem to know it inside and out. Could anyone please help direct me to what I can do to possibly get my issue resolved? Thank you for your time and consideration. attachments enclosed that can be opened in notepad. Bit of code that is a member function of the user defined struct follows: Code:
void token::newTable(token oldTable[])
{
ifstream input;
input.open("text.txt", ios::in);
if (!input)
{
cerr<<"Error opening file:" <<endl;
return;
}
token newTable[5];
int codeCntr=3;
for(int j=0;j<=codeCntr;j++){
cout <<oldTable[j].key <<endl;
newTable[j]=oldTable[j];
cout <<newTable[j].code <<endl;}
int found=0;
int k=0, h=0, first_time=1;//, loopCntr=1;
first_time=0;
//char current, next;
char c[20]="";
char tempString[20]="";
char buffer[20]="";
// char *emptyArray[20]=" ";
ofstream outdata;
outdata.open("outdata.txt");
//buffer[30]=emptyArray[30];
//cout<<buffer<<"<---intial buffer"<<endl;
input.get(*tempString);
strcat(buffer,tempString);
while(!input.eof())
{
cout<< newTable[codeCntr].key<<endl<<endl;
//for(int m=0;m<1;m++)
//{
//buffer=emptyArray;
cout<< newTable[codeCntr].key<<endl;
//}
//for(int m=0;m<5;m++)
//buffer[m]=0;
input.get(*c);
// strcat(buffer,tempString);
strcat(buffer,c);
cout <<c <<"<---c value" <<endl;
cout <<buffer<<"<--buffer value"<<endl;
cout <<tempString <<"<---tempString"<<endl;
found=0;
for(k=0;k<=codeCntr;k++)
{
usleep(1600);
cout <<newTable[k].key <<"<---newTable character"<<endl;;
cout <<buffer<<" b4 if statement" <<endl;
if(char(newTable[k].key)==char(buffer)&&found==0)
{
tempString=buffer;
cout<<"found it**************" <<endl<<k<<endl;
for(int m=0;m<1;m++)
buffer[m]=0;
found=1;
//for(int l=0;l<10;l++)
// buffer[30]=emptyArray[30];
// strcpy(tempString," ");
}
}
if(found==0)
{
h=0;
codeCntr++;
//char bufferHolder[20]=" ";
//bufferHolder=buffer;
newTable[codeCntr].code=codeCntr;
cout<<newTable[codeCntr].code<<" newTable code"<<endl;
cout<< (newTable[codeCntr].key=buffer)<<endl;
cout<< newTable[codeCntr].key<<endl;
strcpy(buffer,"\0");
tempString=c;
cout<<tempString<<"<--tempString in no found" <<endl;
cout<<c<<"<--c value" <<endl;
cout<<codeCntr<<" = newTable.code"<<endl;
//for(int m=0;m<5;m++)
//{
//buffer=emptyArray;
cout<< newTable[codeCntr].key<<endl;
cout<<buffer<<"buffer after delete"<<endl;
cout<< newTable[codeCntr].key<<endl;
cout<<"in !found statement" <<endl;
}
}//end first while loop
//for(int h=0;h<3;h++)
//cout<< newTable[h].key;
input.close();
outdata.close();
return;
}
|
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > Issue implementing algorithm of Lempel Ziv |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|