using stringstream for tokenizing a string

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Apr 2004
Posts: 353
Reputation: Asif_NSU is on a distinguished road 
Solved Threads: 3
Asif_NSU's Avatar
Asif_NSU Asif_NSU is offline Offline
Posting Whiz

using stringstream for tokenizing a string

 
0
  #1
Jul 8th, 2005
  1. string s;
  2. getline(ifile, s, '\n');
  3. stringstream parser(s);
  4. string temp;
  5. while(parser>>temp)
  6. variables.push_back(temp);
  7. getline(ifile, s, '\n');
  8. while(parser>>temp)
  9. 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?
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,433
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 249
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: using stringstream for tokenizing a string

 
0
  #2
Jul 8th, 2005
I think you can do something like this.
  1. 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
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 353
Reputation: Asif_NSU is on a distinguished road 
Solved Threads: 3
Asif_NSU's Avatar
Asif_NSU Asif_NSU is offline Offline
Posting Whiz

Re: using stringstream for tokenizing a string

 
0
  #3
Jul 8th, 2005
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,433
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 249
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: using stringstream for tokenizing a string

 
0
  #4
Jul 8th, 2005
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
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,802
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 747
Team Colleague
Narue's Avatar
Narue Narue is online now Online
Code Goddess

Re: using stringstream for tokenizing a string

 
0
  #5
Jul 8th, 2005
Don't forget that a stringstream is still a stream. This should work for you:
  1. #include <iostream>
  2. #include <sstream>
  3. #include <string>
  4.  
  5. int main()
  6. {
  7. std::stringstream s ( "This is a test" );
  8. std::string token;
  9.  
  10. while ( s>> token )
  11. std::cout<< token <<'\n';
  12.  
  13. // Clear the stream state
  14. s.clear();
  15. // Rewind the stream
  16. s.seekg ( 0, std::ios::beg );
  17.  
  18. while ( s>> token )
  19. std::cout<< token <<'\n';
  20. }
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 353
Reputation: Asif_NSU is on a distinguished road 
Solved Threads: 3
Asif_NSU's Avatar
Asif_NSU Asif_NSU is offline Offline
Posting Whiz

Re: using stringstream for tokenizing a string

 
0
  #6
Jul 9th, 2005
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.
  1. #include<string>
  2. #include<iostream>
  3. #include<sstream>
  4. #include<fstream>
  5. #include<vector>
  6.  
  7. using namespace std;
  8.  
  9. int main()
  10. {
  11. string input;
  12. ifstream ifile("input.txt");
  13.  
  14. getline(ifile,input,'\n');
  15. stringstream parser(input);
  16. string token;
  17.  
  18. vector<string> variables;
  19. while(parser>>token)
  20. variables.push_back(token);//S, A and B have been pushed
  21.  
  22. getline(ifile,input,'\n');//new content in input
  23. parser.clear();
  24. 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
  25. vector<string> terminals;
  26. while(parser>>token)
  27. terminals.push_back(token);//I expect to push 0 and 1, not S, A and B again,
  28.  
  29.  
  30. }

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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,433
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 249
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: using stringstream for tokenizing a string

 
0
  #7
Jul 9th, 2005
   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
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 353
Reputation: Asif_NSU is on a distinguished road 
Solved Threads: 3
Asif_NSU's Avatar
Asif_NSU Asif_NSU is offline Offline
Posting Whiz

Re: using stringstream for tokenizing a string

 
0
  #8
Jul 9th, 2005
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.
  1.  
  2. string line;
  3. stringstream s(line);
  4. string token;
  5. while(s>>token)
  6. cout<<token;
  7.  
  8. s.clear();//Solved my problem after using these three lines together.
  9. s.seekg(0,ios::beg);
  10. s.str(line);
  11.  
  12. while(s>>token)
  13. cout<<token;
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC