Reputation: 45
Is there way to detect end of line in stringstream? My file:
1/2
2/3
3/4
4/5
Something like that is not working:
stringstream buffer;
buffer << file.rdbuf();
string str;
getline(buffer, str);
...
istringstream ss(str);
int num;
ss >> num;
if (ss.peek() == '/') //WORKS AS EXPECTED!
{...}
if(ss.peek() == '\n') //NOT WORKING! SKIPS THIS CONDITION.
{...}
This is was warned:
if(ss.telg() == -1) //WARNED!
~~~~~
{...}
Upvotes: 1
Views: 11625
Reputation: 37
You could always use find_first_of
:
std::string str_contents = buffer.str();
if(str_contents.find_first_of('\n') != std::string::npos) {
//contains EOL
}
find_first_of('\n')
returns the first instance of the EOL character. If there are none, then it returns (a very large index) std::string::npos
. If you know that there is a EOL character in your string, you can get the the first line using
std::string str;
std::getline(buffer, str);
Also see NathanOliver's Answer
Upvotes: 0
Reputation: 595782
std::istringstream
has an eof()
method:
Returns true if the associated stream has reached end-of-file. Specifically, returns true if
eofbit
is set inrdstate()
.
string str;
istringstream ss(str);
int num;
ss >> num;
if (ss.eof()) {...}
Upvotes: 3