字符串流。检测行尾
Stringstream. Detect end of line
有没有办法检测字符串流中的行尾?
我的档案:
1/2
2/3
3/4
4/5
类似的东西不起作用:
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.
{...}
这是警告:
if(ss.telg() == -1) //WARNED!
~~~~~
{...}
std::istringstream
有一个 eof()
方法:
Returns true if the associated stream has reached end-of-file. Specifically, returns true if eofbit
is set in rdstate()
.
string str;
istringstream ss(str);
int num;
ss >> num;
if (ss.eof()) {...}
你总是可以使用 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 EOL 字符的第一个实例。如果有none,那么它returns(一个非常大的索引)std::string::npos
。如果你知道你的字符串中有一个 EOL 字符,你可以使用
获取第一行
std::string str;
std::getline(buffer, str);
另见
有没有办法检测字符串流中的行尾? 我的档案:
1/2
2/3
3/4
4/5
类似的东西不起作用:
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.
{...}
这是警告:
if(ss.telg() == -1) //WARNED!
~~~~~
{...}
std::istringstream
有一个 eof()
方法:
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()) {...}
你总是可以使用 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 EOL 字符的第一个实例。如果有none,那么它returns(一个非常大的索引)std::string::npos
。如果你知道你的字符串中有一个 EOL 字符,你可以使用
std::string str;
std::getline(buffer, str);
另见