如何在解析文本C++时删除注释
how to delete comments while parsing text C++
我试图使用.ppm文件中的"ifstream"来解析C++中的文本,但我希望避免文件中以字符"#"开头并在行尾结束的注释。。我可以用下面的代码跟踪注释字符。。。任何人都可以帮助了解如何在字符"\n"之前忽略其余单词?
string word;
file>>word;
if(strcmp(word, "#")){
//TO DO...Dismiss all characters till the end of the line
}
使用std::getline()
&continue
如果line[0] == '#'
:则为while
循环
std::ifstream file( "foo.txt" );
std::string line;
while( std::getline( file, line ) )
{
if( line.empty() )
continue;
if( '#' == line[0] )
continue;
std::istringstream liness( line );
// pull words out of liness...
}
或者,如果#
可以发生在中线,你可以忽略它之后的一切:
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"
你想在#
之后去掉上面的三个例子吗?
或者,如果行的第一个字符是#
,您是否只将其视为注释?
相关文章:
- 将数组的地址分配给变量并删除
- 删除一个线程上有数百万个字符串的大型哈希映射会影响另一个线程的性能
- C/C++编译器通常会删除重复的库吗
- 从链接列表c++中删除一个项目
- C++如何通过用户输入删除列表元素
- 为什么在C++中使用私有复制构造函数与删除复制构造函数
- 是否需要删除包含对象的"pair"?
- 如何在自删除后将对象设置为nullptr
- 迭代时从向量和内存中删除对象
- 使用函数"remove"删除重复元素
- 如何从多映射中删除特定的重复项
- 运算符C++ "delete []"仅删除 2 个前值
- 删除指向指针的指针是运行时错误吗
- 将指针设置为"nullptr"并不能防止双重删除?
- 为什么示例代码访问IUnknown中已删除的内存
- 如何通过 getter 函数删除矢量的元素?
- 从控制台中删除最后打印的元素
- C++中的线程安全删除
- 如何从存储在std::映射中的std::集中删除元素
- 是否有C++编译器选项允许激进地删除所有函数调用,并将参数传递给具有空体的函数