Sorry if this is asking to much, but could you show me an example of using stringstream. Iv been going by what everyone tell's me, and I always get shifted in diffrent directions.Originally Posted by Daved
Sorry if this is asking to much, but could you show me an example of using stringstream. Iv been going by what everyone tell's me, and I always get shifted in diffrent directions.Originally Posted by Daved
>> It does?
You got me there. Sorry.
>> Sorry if this is asking to much, but could you show me an example of using stringstream. Iv been going by what everyone tell's me, and I always get shifted in diffrent directions.
Different people have their own ways of doing things which unfortunately makes it confusing. You just need to try to understand what's going on as much as possible and make the decision on what sounds like the best way to go.
here is an example using stringstreams. Compile it and run it to see what is going on:Code:#include <iostream> #include <sstream> #include <string> int main() { std::string line = "This is a line of text."; std::istringstream istrm(line); std::string word; while (istrm >> word) std::cout << word << std::endl; std::cout << "---Done---" << std::endl; line = "This,is,a,line,of,text,with,commas"; istrm.clear(); istrm.str(line); while (getline(istrm, word, ',')) std::cout << word << std::endl; std::cout << "---Done---" << std::endl; line = "A Number is 13.5"; istrm.clear(); istrm.str(line); istrm >> word; std::cout << word << std::endl; istrm >> word; std::cout << word << std::endl; istrm >> word; std::cout << word << std::endl; double number; istrm >> number; std::cout << number + 1.0 << std::endl; std::cout << "---Done---" << std::endl; }
theres always boost::tokeniser you can get it at here
Free the weed!! Class B to class C is not good enough!!
And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi
Ok thanks, but there's one little part im confused. Where you haveDifferent people have their own ways of doing things which unfortunately makes it confusing. You just need to try to understand what's going on as much as possible and make the decision on what sounds like the best way to go.
here is an example using stringstreams. Compile it and run it to see what is going on:directions.I understand thats a case for the istringstream class, but whats with the (line) part? Sorry if this is a stupid question . I bought a 1000 page book "Sam's Teach Yourself C++ in 21 Days". It wasn't specific on these topics.Code:std::istringstream istrm(line);
When you construct an input string stream, it should have some data in it to extract into your variables. It's just like an ifstream that has the data in the file that can be extracted into variables. The constructor of the istringstream takes a string that you will parse. So I created an example string to parse ("This is a line of text.") and passed that string to the constructor of the istringstream. So now the istringstream is loaded with that string and you extract data out of it just like it was a file with that data inside it.
Notice later I called istrm.str(line). This basically does the same thing, except it is for a stream instance that has already been constructed. It resets the internal contents of the stream to whatever line holds.
If you understand file streams, just think of a stringstream as the same thing, except instead of an actual file on disk, there is just a big string in memory that holds all the data and can be accessed bythe str() method.