| | |
using stringstream for tokenizing a string
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
C++ Syntax (Toggle Plain Text)
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?
I think you can do something like this.
C++ Syntax (Toggle Plain Text)
parser.str("");
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
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.
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?
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Don't forget that a stringstream is still a stream. This should work for you:
C++ Syntax (Toggle Plain Text)
#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'; }
I'm here to prove you wrong.
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.
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.
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.
C++ Syntax (Toggle Plain Text)
#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; "One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
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.
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.
C++ Syntax (Toggle Plain Text)
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;
![]() |
Similar Threads
Other Threads in the C++ Forum
- Previous Thread: Linked stack modification issue
- Next Thread: Error reading file
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api array arrays based beginner binary bmp c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete deploy desktop directshow dll download dynamic encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib library linkedlist linker list loop looping loops map math matrix memory microsoft newbie news number output pointer problem program programming project python random read recursion recursive reference simple string strings studio system temperature template templates test text text-file tree unix url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






