-
December 15th, 2012, 05:59 AM
#1
osstream doesn't flush or clear
Code:
std::ostringstream ss;
ss.flush();
ss.clear();
ss << v.x;
std::string buff(ss.str());
XMLString::transcode(buff.c_str(), tempStr, 99);
elem1->setAttribute(L"X", tempStr);
e1->appendChild(elem1);
ss.flush();
ss.clear();
ss << v.y;
buff.clear();
buff = ss.str();
XMLString::transcode(buff.c_str(), tempStr, 99);
elem1->setAttribute(L"Y", tempStr);
ss.flush();
ss.clear();
ss << v.z;
buff.clear();
buff = ss.str();
XMLString::transcode(buff.c_str(), tempStr, 99);
elem1->setAttribute(L"Z", tempStr);
The buff at "Z" are stringed together with previous values.
How do I resolve that?
Thanks
Jack
-
December 15th, 2012, 08:09 AM
#2
Re: osstream doesn't flush or clear
When you write this kind of repetitive code, the first thought that should come to your mind should be: I'm doing something wrong. This kind of code is hard to maintain and badly conveys what you actually want to do. If you create some functions to take care of the repetitive stuff, you'll code become much clearer, less prone to errors and easier to maintain.
Code:
template <class T>
std::string ToStr(const T& value)
{
std::ostringstream oss;
oss << value;
return oss.str();
}
void SetXmlAttribute(ElemType& elem, const std::string& name, const std::string& value)
{
//...
}
void foo(const Whatever& v)
{
e1->appendChild(elem1);
SetXmlAttribute(elem1, "X", v.x);
SetXmlAttribute(elem1, "Y", v.y);
SetXmlAttribute(elem1, "Z", v.z);
}
As to your actual question; I don't believe flush does anything on a stringstream and clear only clears the error state of the stream. The easiest is to just create a new stringstream instance.
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
-
December 15th, 2012, 08:16 AM
#3
Re: osstream doesn't flush or clear
I agree with the previous post ... create a function (so that new instances are created).
to clear a stringstream, you can use:
-
December 16th, 2012, 03:29 AM
#4
Re: osstream doesn't flush or clear
2 problems on hand
Code:
template <class T>
std::string ToStr(const T& value)
{
std::ostringstream oss;
oss << value;
return oss.str();
}
void SimExporter::SetXmlAttribute(DOMElement* elem, const std::string& name, float value)
{
std::string str = ToStr<float>(name);
return str;
}
void SimExporter::SetXYZ(DOMElement* parent, DOMElement* child, const Point3& v)
{
parent->appendChild(child);
SetXmlAttribute(child, "X", v.x);
SetXmlAttribute(child, "Y", v.y);
SetXmlAttribute(child, "Z", v.z);
}
1) How do I call ToStr from SetXMLAttribute, the above code doesn't compile
2) Isn't it a good idea to use DOMElement * instead of DOMElement& because the interface requires a pointer as the argument.
Thanks
Jack
-
December 16th, 2012, 03:53 AM
#5
Re: osstream doesn't flush or clear
Originally Posted by lucky6969b
1) How do I call ToStr from SetXMLAttribute, the above code doesn't compile
You don't need to specify the template argument explicitly. Just call ToStr(value).
Originally Posted by lucky6969b
2) Isn't it a good idea to use DOMElement * instead of DOMElement& because the interface requires a pointer as the argument.
Either way can be argued for. Your call.
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
-
December 16th, 2012, 05:48 AM
#6
Re: osstream doesn't flush or clear
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|