简体   繁体   中英

How to clear std::ostream

How do we clear or empty a std::ostream object?

The examples I have found here are for stringstream;

stringstream m;
m.str("");

Obviously that does not work with ostream.

EDIT: as asked, for usage:

I use the ostream object in a simple Stopwatch. To trace the time of slow functions. With the time the stream object is getting bigger and bigger, and the app goes unresponsive. So here I am trying to clear the ostream object. Preferable in the constructor of CStopUhr.

static tstringstream fout;                         // fout to stringstream
//static tofstream fout(_T("SpooferTimeTrace.log); // fout to file 
static std::wostream fout(std::wcout.rdbuf());     // fout to cout

class CStopUhr
{
public:
    CStopUhr(tostream& os, LPCTSTR txt = _T(" Time: ")) 
     :_os(os) 
      { _os.clear(); _txt = txt; _dwStart = GetTickCount(); }
    ~CStopUhr()         
      { _os << _txt << (GetTickCount()-_dwStart) << _T("ms ") << std::endl; }
private:
    DWORD _dwStart;
    tostream& _os;
    tstring  _txt;
};

usage:

 void testfunction()
 {
    CStopUhr suhr(fout, CString(__FUNCTION__);
    :
    ;
}

我看不出你这样做的一个很好的理由,但也许你可以直接干扰底层的std::streambuf对象,以碰撞put指针直到缓冲区中没有更多突出的字符?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM