Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect when at the end of a stringstream

Tags:

I am trying to write a function which will detect when I'm almost at the end of a stringstream, but it doesn't seem to work.

Here's some code:

std::string path(1.2.3); int number; std::stringstream ss(path); while (!ss.eof()) {     if (ss.peek() != '.') {         ss >> number;         if (ss.tellg() == path.length()) {             std::cout << "Last one: " << number;         } else {             std::cout << number;         }     } else { ss.get(); } } 

I tried using ss.tellg() == path.length(), but that doesn't work. Does somebody have an alternative?

like image 781
JNevens Avatar asked Dec 13 '13 17:12

JNevens


1 Answers

I figured it out..

std::string path(1.2.3); int number; std::stringstream ss(path); while (!ss.eof()) {     if (ss.peek() != '.') {         ss >> number;         if (ss.tellg() == -1) {             std::cout << "Last one: " << number;         } else {             std::cout << number;         }     } else { ss.get(); } } 
like image 180
JNevens Avatar answered Sep 21 '22 09:09

JNevens