how to reuse stringstream

stringstream c++
stringstream clear
stringstream getline
empty stringstream
stringstream delimiter
stringstream fail
stringstream width
stringstream eof

These threads do NOT answer me:

resetting a stringstream

How do you clear a stringstream variable?

        std::ifstream file( szFIleName_p );
        if( !file ) return false;

        // create a string stream for parsing

        std::stringstream szBuffer;

        std::string szLine;     // current line
        std::string szKeyWord;  // first word on the line identifying what data it contains

while( !file.eof()){

            // read line by line

            std::getline(file, szLine);

            // ignore empty lines

            if(szLine == "") continue;

            szBuffer.str("");
            szBuffer.str(szLine);
            szBuffer>>szKeyWord;

szKeyword will always contain the first word, szBuffer is not being reset, can't find a clear example anywhere on how to use stringstream.

New code after answer:

...
            szBuffer.str(szLine);
            szBuffer.clear();
            szBuffer>>szKeyWord;
...

Ok, thats my final version:

        std::string szLine;     // current line
        std::string szKeyWord;  // first word on the line identifying what data it contains

        // read line by line

        while( std::getline(file, szLine) ){

            // ignore empty lines

            if(szLine == "") continue;

            // create a string stream for parsing

            std::istringstream szBuffer(szLine);
            szBuffer>>szKeyWord;

You didn't clear() the stream after calling str(""). Take another look at this answer, it also explains why you should reset using str(std::string()). And in your case, you could also reset the contents using only str(szLine).

If you don't call clear(), the flags of the stream (like eof) wont be reset, resulting in surprising behaviour ;)

Re-use stringstream - C++ Forum, seekg(0,ios::beg); //Move the get pointer to the beginning of the stringstream fileContents.clear(); //Reset the flags only while (!fileContents.eof()) {  Worth pointing out that this won't re-use the underlying buffer from the ostringstream - it just assigns a new buffer. So while you're reusing the ostringstream object, you're still allocating two buffers. I don't think ostringstream is designed for reuse in the manner you intend. – razlebe Mar 8 '09 at 21:09


It depends what you're doing with it. It's generally easier to just create a new istringstream or ostringstream. To "reset" a stream, you have to clear its buffer, clear any error flags, reset any formatting flags, plus the precision and fill, reimbue it with the original locale, and not forget any extended formatting information generated with a value returned from xalloc. In sum, impossible to get correct.

And while I'm at it, your loop is wrong, and will probably result in the last line being processed twice. file.eof() only has a usable meaning after the input has failed (and even then, it's not 100% reliable). What you want is:

std::string line;
while ( std::getline( file, line ) ) {
    if ( !line.empty() ) {
        std::istringstream buffer( line );
        //  ...
    }
}

(Actually, you probably want to trim trailing white space from the line before the test for empty.)

reusing (string) streams Solutions, I want to use std::stringstream to perform text formatting, rather than the fat CString or the bit-hacking sprintf. I like using the operator<< format conversions in​  Here we will see the string stream in C++. The string stream associates a string object with a string. Using this we can read from string as if it were a stream like cin. The Stringstream has different methods. These are like below − clear (): Used to clear the stream. str (): To get and set the string object whose content is present in stream.


how to reuse stringstream, These threads do NOT answer me: resetting a stringstream How do you clear a stringstream variable? std::ifstream file( szFIleName_p ); if( !file ) return false;  stringstream (stringstream&& x); Construct object. Constructs a stringstream object: (1) empty constructor (default constructor) Constructs a stringstream object with an empty sequence as content. Internally, its iostream base constructor is passed a pointer to a stringbuf object constructed with which as argument. (2) initialization constructor.


std:stringstream reset - C / C++, Giampiero Gabbiani wrote: Is it possible to reset a std::stringstream in order to reuse it once more? I tried with flush() method without any success.. A stringstream associates a string object with a stream allowing you to read from the string as if it were a stream (like cin). str () — to get and set string object whose content is present in stream. operator << — add a string to the stringstream object. stringstream class is extremely useful in parsing input.


If you have the stream in a class member, use unique_ptr<stringstream>, then just reset(new stringstream(...)) to reuse it.

std::stringstream, reuse of, In my code i have a whole bunch of the following uses of std::stringstream to convert data to stings and then send the string data, so some  Stringstream is a stream, so its iterators are forward, cannot return. In an output stringstream, you need a flush() to reinitialize it, as in any other output stream.


18.4, 12345 67.89. Clearing a stringstream for reuse. There are several ways to empty a stringstream's buffer. 1) Set it to the empty string using str()  typedef basic_stringstream<char> stringstream; Stream class to operate on strings. Objects of this class use a string buffer that contains a sequence of characters. This sequence of characters can be accessed directly as a string object, using member str.


[PDF] Stringstreams & Parsing, C++ Stringstream Reuse. • Beware of re-using the same stringstream object for multiple conversions. It can be weird. – Make sure you clear it out between uses  stringstream basically allows you to treat a string object like a stream, and use all stream functions and operators on it. I saw it used mainly for the formatted output/input goodness. One good example would be c++ implementation of converting number to stream object.


C++ String Streams, The stringstream, ostringstream, and istringstream objects are used for input and output to a string. They behave in a manner similar to fstream, ofstream and  How to use stringstream to separate comma separated strings [duplicate] Ask Question Asked 7 years, 9 months ago. Active 1 year, 2 months ago.