Clearing stringstream

Hi, How do I clear a stringstream. I have tried this site and others and have not found anything that works. I am making a tokenizer for a "fun" programming language that I am creating as a learning exercise.
I tokenize the file line by line, but I cant seem to clear the string stream to accept the next line (The SS is global)

1
2
3
4
5
6
7
8
9
10
11
12
while (!inputFile.eof())
    {
        //currentLine.str("");
        //currentLine.clear();
        currentLineString = getLineFromFile();
        cout << "Current Line: " << currentLineString << endl;
        currentLine.clear();
        currentLine.str(currentLineString);
        cout << "CLEOF: " << currentLine.eof() << endl;
        tokenLine();
        cout << "CLEOF: " << currentLine.eof() << endl;
    }


As you can see, I have tried several solutions to no avail. Any help is appreciated.

Sidenote: I am using Code::Blocks on 64-Bit linux with the gnu gcc compiler
Last edited on
Use something like this:
1
2
3
std::stringstream currentLine;
currentLine.str("");
currentLine(std::string()); // this do the same thing 
Last edited on
^^I have already tried this, and it does not work. Also, as aforementioned the stringstream is global.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <sstream>
#include <fstream>

std::istringstream currentLine ;

void foo()
{
    std::string token ;
    while( currentLine >> token ) std::cout << "    token: " << token << '\n' ;
    // the stream currentLine is in a failed state at this point
    std::cout << '\n' ;
}

int main()
{
    std::ifstream file(__FILE__) ;

    std::string line ;
    int cnt = 0 ;
    while( std::getline( file, line ) )
    {
        std::cout << "line " << ++cnt << "\n----------\n" ;
        currentLine.clear() ; // *** important *** clear the failed state
        currentLine.str(line) ; // reset the buffer to the line just read
        foo() ;
    }
}

http://coliru.stacked-crooked.com/a/48472b16add3ca7a
Topic archived. No new replies allowed.