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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
ASKER
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
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
ASKER
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=n ew Date("10","11","1983");
print(dateObj);
print(Object *obj)
{
obj=new Object();
cout<<obj->toString()<<end l;
}
It works. Which one do you think is better and why? Thanks.
Object *obj;
void print(Object*);//dateObj=n
print(dateObj);
print(Object *obj)
{
obj=new Object();
cout<<obj->toString()<<end
}
It works. Which one do you think is better and why? Thanks.
ASKER
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.
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.
ASKER
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();?
>>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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ostr.flush();