Here is the decompress function (part)
void decompress(ifstream& fin,ofstream& fout)
{
int next_code = FIRST_CODE,dict_size=next_code-1;
//int flag=0;
string str_cur, str_prev,str_buffer,xxx;
xxx.clear();
str_buffer.clear();
for (int i = 0 ; i < (MAX_SYM+1) ; i++ )
{
dict[i].character= (char)i;
dict[i].code_value = i; //code values are same as i
dict[i].parent_code = UNUSED;
}
for (int i = (MAX_SYM+1) ; i < TABLE_SIZE ; i++ )
{ //initialize all code values to -1
dict[i].code_value = UNUSED;
dict[i].parent_code = UNUSED;
}
char chr;
bits_left=0; ch_glob=0x0000; //GLOBAL VAR INITIALIZED
int code=get_code_from_file(fin); //returns a 12 bit code value
str_cur=find_string(code,dict_size,xxx); //1st character should be in dictionary
cout<<"\nString found: "<<str_cur<<" for code: "<<code<<endl;
system("pause");
fout<<str_cur;
//str_buffer=str_cur;
do{
while(find_code(str_cur,dict_size) != -1) //input ch until string not in dictionary
{
if(str_buffer.length()>0) //if buffer is not empty, no need to read from file
{
cout<<"buffer length > 0\n";
chr=str_buffer[0];
str_buffer.erase(0,1);
}
else
{
code= get_code_from_file(fin);
if (code == 256) //eof reached
{
break;
}
str_buffer=find_string(code,dict_size,xxx); //before coming here buffer length will be 0
cout<<"\nString found: "<<str_buffer<<" for code: "<<code<<endl;
system("pause");
fout<<str_buffer; //write decoded string immediately as it is found
//if (str_buffer.length() > 0) //check whether returned string is a single character or not
//{
chr=str_buffer[0];
str_buffer.erase(0,1);
//}
}
cout<<"\nStr_cur before appending is: "<<str_cur;
cout<<"\n character to be appended is: "<<chr;
str_prev=str_cur;
*(&chr+1)='\0'; *******see this line
str_cur = str_cur.append(&chr);
cout<<"\nchecking dictionary for existence of string: "<<str_cur<<endl;
system("pause");
}
i am trying to append a character to the string.
suppose str_cur had "p"
and ch had "r"
when i append without the highlighted(with * line ) i get "prr" ?
why???

New Topic/Question
Reply



MultiQuote




|