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.
LVL 5
b_vishwajitAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jhshuklaCommented:
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
pb_indiaCommented:
use:

ostr.flush();
0
b_vishwajitAuthor Commented:
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.
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

b_vishwajitAuthor Commented:
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.
0
jhshuklaCommented:
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
0
b_vishwajitAuthor Commented:
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.
0
b_vishwajitAuthor Commented:
does seekp(arg); work for istringstream too? I have'nt tried it yet. Or do I have to use seekg(); Thanks.
0
itsmeandnobodyelseCommented:
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.

0
b_vishwajitAuthor Commented:
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();?
0
itsmeandnobodyelseCommented:
>> 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

   
0
itsmeandnobodyelseCommented:
Whats "that"? U mean seekg();?

No, i mean istringstream::str(string s) function, that would clear the buffer if you are passing an empty string.

Actually, seekg is a virtual function defined in istream class and i don't know if there is an appropriate overload in istringstream.

Regards, Alex


0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.