如何在解析文本C++; 我试图用一个.ppm文件解析“C++流”中的文本,但是我想避免在文件中用字符“y*”开头的注释,在结尾行结束。我可以用下面的代码跟踪评论字符……任何人都可以帮助我们消除单词的余数直到字符“n”?< /p> string word; file>>word; if(strcmp(word, "#")){ //TO DO...Dismiss all characters till the end of the line }
使用如何在解析文本C++; 我试图用一个.ppm文件解析“C++流”中的文本,但是我想避免在文件中用字符“y*”开头的注释,在结尾行结束。我可以用下面的代码跟踪评论字符……任何人都可以帮助我们消除单词的余数直到字符“n”?< /p> string word; file>>word; if(strcmp(word, "#")){ //TO DO...Dismiss all characters till the end of the line },c++,ifstream,text-parsing,ppm,C++,Ifstream,Text Parsing,Ppm,使用std::getline()和继续while循环,如果行[0]=='#': 或者,如果#可能发生在中间,您可以忽略它之后的所有内容: std::ifstream file( "foo.txt" ); std::string line; while( std::getline( file, line ) ) { std::istringstream liness( line.substr( 0, line.find_first_of( '#' ) ) ); // pull wo
std::getline()
和继续while
循环,如果行[0]=='#'
:
或者,如果#
可能发生在中间,您可以忽略它之后的所有内容:
std::ifstream file( "foo.txt" );
std::string line;
while( std::getline( file, line ) )
{
std::istringstream liness( line.substr( 0, line.find_first_of( '#' ) ) );
// pull words out of liness...
}
根据您想删除的注释的复杂性,可以考虑使用正则表达式:
例如,以下哪项将被视为评论:
# Start of line comment
Stuff here # mid-line comment
Contact "Tel# 911"
您想在#
之后去掉以上三个示例吗
或者,如果行的第一个字符是#
?可能最好使用std::getline()
作为案例的主要输入源,您才将其视为注释。可以使用std::istringstream
进一步检查非注释行。是否保证“#”始终位于第0个位置?我正在考虑这两种情况您可以使用正则表达式,例如(?:“(?:[^”\]\\\)*“|[^”\]*(#$)
来捕获注释
# Start of line comment
Stuff here # mid-line comment
Contact "Tel# 911"