C++ 从字符串打印HTML标记

C++ 从字符串打印HTML标记,c++,c++11,C++,C++11,我正在尝试打印字符串中包含的所有html标记。这里的代码逻辑似乎出了问题,我不断得到一个永无止境的循环 string fileLine; string strToPush; fileLine ="<html> sdad </b>as"; // My string that I want to find tags from cout << fileLine << '\n'; while(!fileLine.length()==0)

我正在尝试打印字符串中包含的所有html标记。这里的代码逻辑似乎出了问题,我不断得到一个永无止境的循环

string fileLine;
string strToPush;

fileLine ="<html> sdad </b>as"; // My string that I want to find tags from

  cout << fileLine << '\n';

  while(!fileLine.length()==0)
  {

      if(fileLine.at(0)=='<')
        { 
            strToPush = "";
            while(!fileLine.at(0)=='>') 
            {
            strToPush = strToPush + fileLine.at(0);
            fileLine.erase (0); 
            }

            cout << endl << "HTML Tag detected: " << strToPush <<endl;

        }
        else
        {
        fileLine.erase (0);
        }
  }
字符串文件行;
字符串strToPush;
fileLine=“sdad as”//我要从中查找标记的字符串

我怀疑你被操作员的优先权绊倒了。线路

while(!fileLine.length()==0)
相当于

while( (!fileLine.length()) == 0 )
你可能想做的是:

while( !(fileLine.length() == 0) )
您可以将其简化为:

while( !fileLine.empty() )
同样地,更改行

while(!fileLine.at(0)=='>') 


您应该使用HTML或XML解析器,这样就不必浪费时间调试功能。我怀疑您的问题在于涉及
的比较
==
。例如,您的while循环。。。你真的打算这么做吗!fileline.length()==0
?或者你真的想要
fileline.length()!=0
甚至
!(fileline.length()==0)
。在您编写时,
!fileline.length()
将在长度为零时计算为true,在长度为非零时计算为false。比较true==0将为false,false==0将为true。。。令人困惑,不是吗?如果要检查长度是否不为零,请使用不相等运算符:
=
如果您只想搜索模式
,那么regex可能会有所帮助。但这也会报告注释中出现的情况,而不会验证任何内容。如果您需要更多功能,请按照上面的建议使用专用库。另外,行:
while(!fileLine.at(0)='>')
可能会有类似的问题。这似乎是问题所在,也证明我使用的erase()方法不正确。
while ( fileLine.at(0) != '>' )