?
Solved

ostringstream and istringstream (An easy question).

Posted on 2004-11-11
11
Medium Priority
?
730 Views
Last Modified: 2008-02-01
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.
0
Comment
Question by:b_vishwajit
  • 5
  • 3
  • 2
  • +1
11 Comments
 
LVL 9

Accepted Solution

by:
jhshukla earned 400 total points
ID: 12557309
0
 
LVL 2

Expert Comment

by:pb_india
ID: 12557447
use:

ostr.flush();
0
 
LVL 5

Author Comment

by:b_vishwajit
ID: 12557481
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 5

Author Comment

by:b_vishwajit
ID: 12557523
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
 
LVL 9

Expert Comment

by:jhshukla
ID: 12557803
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
 
LVL 5

Author Comment

by:b_vishwajit
ID: 12557963
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
 
LVL 5

Author Comment

by:b_vishwajit
ID: 12557981
does seekp(arg); work for istringstream too? I have'nt tried it yet. Or do I have to use seekg(); Thanks.
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 12558225
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
 
LVL 5

Author Comment

by:b_vishwajit
ID: 12558259
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
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 12558330
>> 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
 
LVL 39

Assisted Solution

by:itsmeandnobodyelse
itsmeandnobodyelse earned 100 total points
ID: 12558435
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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question