[英]getline crashing when coming across a newline
I am trying to read in some informations from txt file with \\n
endings. 我正在尝试从带有
\\n
结尾的txt文件中读取一些信息。 However whenever I come a cross an empty line, I get a seg fault. 但是,每当我越过空行时,都会出现段错误。 However I just want the line to get ignored.
但是,我只是希望该行被忽略。
code: 码:
std::ifstream config_file (config_);
string input_line;
while (std::getline(config_file, input_line))
{
if (??check for newline??)
continue
}
I tried so far: changing getline to these parameters: 到目前为止,我尝试过:将getline更改为以下参数:
(config_file, input_line, '\n')
and this if statement: 这是if语句:
if (input_line.at(0) == '\n')
However I always get seg faults ^^'. 但是我总是遇到段错误^^'。
Use of 用于
if (input_line.at(0) == '\n')
to check whether is an empty line is wrong sincce std::getline
reads and discards the delimiter ( '\\n'
in your case). 从
std::getline
读取并丢弃定界符(在您的情况下为'\\n'
,以检查是否为空行是错误的。
Instead, use 相反,使用
if (input_line.empty())
std::getline
will discard the newline. std::getline
将放弃换行符。 You can check for std::string::empty()
to detect an empty line. 您可以检查
std::string::empty()
来检测空行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.