CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Dec 2010
    Posts
    907

    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

  2. #2
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    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

  3. #3
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,721

    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:

    Code:
    ss.str("");

  4. #4
    Join Date
    Dec 2010
    Posts
    907

    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

  5. #5
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: osstream doesn't flush or clear

    Quote Originally Posted by lucky6969b View Post
    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).
    Quote Originally Posted by lucky6969b View Post
    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

  6. #6
    Join Date
    Dec 2010
    Posts
    907

    Re: osstream doesn't flush or clear

    Thanks, it's all great

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured