string s;
   getline(ifile, s, '\n');
   stringstream parser(s);
   string temp;
   while(parser>>temp)
      variables.push_back(temp);
   getline(ifile, s, '\n');
   while(parser>>temp)
      terminals.push_back(temp);//Doesn't work the second time.

stringstream can tokenize the string s only for the first time. The string s is passed to it through the constructor. How can reinitialize the stringstream object?

Recommended Answers

All 7 Replies

I think you can do something like this.

parser.str("");

Sorry Dave, but I still could not get it to work. I want to tokenize the string s for the second and consecutive times. But it works only for the first time. After all the tokens in s have been tokenized the 'parser' seems to be empty. I tried both with
parser.str("") and parser.str(s.c_str()), but still no use.

Do you have a small compileable snippet and a short sample of an input file?

Don't forget that a stringstream is still a stream. This should work for you:

#include <iostream>
#include <sstream>
#include <string>

int main()
{
  std::stringstream s ( "This is a test" );
  std::string token;

  while ( s>> token )
    std::cout<< token <<'\n';

  // Clear the stream state
  s.clear();
  // Rewind the stream
  s.seekg ( 0, std::ios::beg );

  while ( s>> token )
    std::cout<< token <<'\n';
}

say for example the input.txt file will be like this

S A B
0 1

The first line will consist of variables, the second line with terminals. They do have to be of type char, can also be strings.

#include<string> 
#include<iostream> 
#include<sstream> 
#include<fstream>
#include<vector>

using namespace std;

int main()
{
	string input;
	ifstream ifile("input.txt");

	getline(ifile,input,'\n');	
	stringstream parser(input);
	string token;
	
	vector<string> variables;
	while(parser>>token)
		variables.push_back(token);//S, A and B have been pushed

	getline(ifile,input,'\n');//new content in input
	parser.clear();
	parser.seekg ( 0, std::ios::beg );//clearing and resetting the get ptr doesnt let me use the new content of string input, still the previous string
	vector<string> terminals;
	while(parser>>token)
		terminals.push_back(token);//I expect to push 0 and 1, not S, A and B again,
	
	
}

Let's say I have two strings.
string a;
string b;
Now,
stringstream s(a); //This stringstream object will work with string a;
The question is how do I let it work with string b, after I am done with a?
I have never worked with stringstream class before, so I am not very familiar how they are actually used.

parser.clear();
   parser.seekg ( 0, std::ios::beg );//clearing and resetting the get ptr doesnt let me use the new content of string input, still the previous string
   getline(ifile,input);//new content in input
   parser.str(input);
   vector<string> terminals;

Just after my last post i discovered it and wanted to post this one, but for some error occured. Dave and Narue thank you both.

Ha ha ha; okay I got it. Using Dave's code alone did not work. Using Narue's code alone also did not work. However, my problem was solved after I used both of ur solution together.

string line;
 stringstream s(line);
 string token;
 while(s>>token)
    cout<<token;
 
 s.clear();//Solved my problem after using these three lines together.
 s.seekg(0,ios::beg);
 s.str(line);
 
 while(s>>token)
    cout<<token;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.