5 Replies - 832 Views - Last Post: 29 September 2010 - 03:00 AM Rate Topic: -----

#1 angel.c   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 12-September 10

Append() fxn of string library

Posted 28 September 2010 - 12:11 PM

I am writing a code for LZW function.
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???

Is This A Good Question/Topic? 0
  • +

Replies To: Append() fxn of string library

#2 eker676   User is offline

  • Software Engineer
  • member icon

Reputation: 379
  • View blog
  • Posts: 1,833
  • Joined: 18-April 09

Re: Append() fxn of string library

Posted 28 September 2010 - 12:29 PM

Why don't you just use the += syntax?

str_cur += ch;

If you really want to use append, remember that C++ is not like java or C# where strings are immutable.

You also have to specify how many characters are being appended.

str.append(character, 1);

This post has been edited by eker676: 28 September 2010 - 12:34 PM

Was This Post Helpful? 0
  • +
  • -

#3 angel.c   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 12-September 10

Re: Append() fxn of string library

Posted 28 September 2010 - 12:42 PM

str_cur += ch; gives error. Obviously i tried that before going to append.

.append(ch,1) solves the problem. Thanks
The pointer way is very dangerous.

However any reson why the problem occurred?
(maybe because i erased a character before and that was somewhere after ch in memory??)
Was This Post Helpful? 0
  • +
  • -

#4 Oler1s   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1397
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Append() fxn of string library

Posted 28 September 2010 - 01:03 PM

*(&chr+1)='\0'; is not valid. Before: char chr; . &chr is valid. &chr + 1 is not.
Was This Post Helpful? 0
  • +
  • -

#5 angel.c   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 12-September 10

Re: Append() fxn of string library

Posted 29 September 2010 - 02:06 AM

You are telling me that *(&chr + 1) is no valid. Dude, it is compiling and running WITOUT ANY ERROR on my machine. It is dangerous though. Any reason why you thought so???
Was This Post Helpful? 0
  • +
  • -

#6 janotte   User is offline

  • code > sword
  • member icon

Reputation: 991
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Append() fxn of string library

Posted 29 September 2010 - 03:00 AM

Put a little bit of debug code in your program that displays out what "&char" is to your screen.
Did you get the output you expected?
Now output "&char + 1".
Did you get the output you expected?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1