C++;tokeniser,跳过以'//';以及特设'/**/'; 我正在练习用C++编写一个记录器,但是,我假设我需要把一些注释作为输入文件。那我怎么能跳过下面图片中的评论呢。 有什么想法吗?
对于C++;tokeniser,跳过以'//';以及特设'/**/'; 我正在练习用C++编写一个记录器,但是,我假设我需要把一些注释作为输入文件。那我怎么能跳过下面图片中的评论呢。 有什么想法吗?,c++,C++,对于/使用regex库,您可以: // for "//" for( std::string line; std::getline( input_file_stream, line ); ){ if( std::regex_search( line, std::regex( R"(^//)" ) ) ){ // nothing } else { std::cout << line << '\n'; }
/
使用regex
库,您可以:
// for "//"
for( std::string line; std::getline( input_file_stream, line ); ){
if( std::regex_search( line, std::regex( R"(^//)" ) ) ){
// nothing
}
else {
std::cout << line << '\n';
}
}
std::ifstream input_file_stream( "file" );
// for // and /* up to */
for( std::string line; std::getline( input_file_stream, line ); ){
if( *line.begin() == '/' ){
if( *(line.begin() + 1) == '*' ){
while( *line.begin() != '*' && *(line.begin() + 1 ) != '/' ){
std::getline( input_file_stream, line );
}
}
} else {
std::cout << line << '\n';
}
}
input_file_stream.close();
测试,输入:
// skip
// skip
okay
// skip
okay
okay
/* skip up to
not
not
not
*/
okay
the end
okay
okay
okay
okay
the end
和输出:
// skip
// skip
okay
// skip
okay
okay
/* skip up to
not
not
not
*/
okay
the end
okay
okay
okay
okay
the end
起初我尝试了一种方法,比如,读取第一个标记字符,如果是“/”,则继续读取下一个字符,直到行尾。但它只适用于第一行。您会接受基于sed的解决方案吗?或者如果您坚持使用awk、perl等?您想让我们编写代码?请通过编辑您的问题提供更多信息。啊,是的,您是对的@EdHeal。你好,欢迎来到StackOverflow。请参加stackoverflow.com/tour之旅,学习提出好问题stackoverflow.com/help/how-to-ask,制作一个MCVE stackoverflow.com/help/MCVE