Link to home
Start Free TrialLog in
Avatar of b_vishwajit
b_vishwajit

asked on

ostringstream and istringstream (An easy question).

I am trying to use a ostringstream object from sstream library file. I am using << to perform a formatted output operation. Using ostr<<(arg) operation will cause ostr to store the argument as  a string. Then I use ostr.str() function to read the arg into a string object. Now my problem is I want to clear whatever has been stored in ostr object and pass another arguement to it. How can I do it? Reply asap.Thanks.
ASKER CERTIFIED SOLUTION
Avatar of jhshukla
jhshukla
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of pb_india
pb_india

use:

ostr.flush();
Avatar of b_vishwajit

ASKER

I am gonnna try that. I have another doubt: If I have an object declared as Object  *obj in my main program then how would I pass this object to function to called print() to create and initialize the object and then print its data members. Reply. Thanks.
ostr.flush() does not work. In fact I tried ostr.flush() before posting my question on this forum. But seekp(0) does work. Thanks jhshukla. Now can some one clarify my other doubt of passing objects to functions.
Object *obj;
print(&obj);

void print(Object **pObj){
  *pObj = new Object();
  (*pObj)->field1 = some_value1;
  ...
  cout
    << (*pObj)->field1 << endl
    << (*pObj)->field2 << endl
    ......
    << (*pObj)->fieldn << endl;
}

if you will be printing the contents quite often then you should consider overloading the << operator. gtg to class. will give details after coming back.

happy programming.
jaydutt
Thank you again for replying. I did it the following way: (took me one hour to figure it out since I am newbie to c++)
      Object  *obj;
      void print(Object*);//dateObj=new Date("10","11","1983");
      print(dateObj);

                print(Object *obj)
                {
                          obj=new Object();
                          cout<<obj->toString()<<endl;
                 
                }

It works. Which one do you think is better and why? Thanks.
does seekp(arg); work for istringstream too? I have'nt tried it yet. Or do I have to use seekg(); Thanks.
That works also:

     ostringstream oss;
     string s;
     oss << "A string";
     cout << oss.str() << endl;
     oss.str(""); // make string empty
     oss << "Another string";
     cout << oss.str() << endl;

Regards, Alex

P.S. Should work for istringstream too.

Thank you Alex. I will try your suggestion for istringstream as seekp() works for ostringstream.You said:
>>That works also:

Whats "that"? U mean seekg();?
>> how would I pass this object to function to called print()

   void print(Object*& pObj)
   {
         pObj = new Object(arg1, arg2, arg3); // init your data members in the constructor
         cout << *pObj;
   }

For this you need ostream::operator<< like that:

  ostream& operator<< (ostream& os, const Object& obj) const
  {
         os << obj.getFirstMember() << " " << obj.getSecondMember() << .... << endl;
  }

Make a forward declaration in the class header and implementation either as inline in the header too or in the cpp file of class Object.

Regards, Alex

   
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial