C++ 为什么是无限循环?
我有一个类,它接受html文件并格式化它。这是我的密码C++ 为什么是无限循环?,c++,file,C++,File,我有一个类,它接受html文件并格式化它。这是我的密码 void FormatHtml::Format(const std::string &formattedFile, const std::string &inputFile) const { string str; ifstream inputfileObj(inputFile.c_str()); //ofstream formattedFileObj(formattedFile.c_str());
void FormatHtml::Format(const std::string &formattedFile, const std::string &inputFile) const
{
string str;
ifstream inputfileObj(inputFile.c_str());
//ofstream formattedFileObj(formattedFile.c_str());
if(inputfileObj.is_open() /*&& formattedFileObj.is_open()*/)
{
while(inputfileObj.good())
{
getline(inputfileObj,str);
//cout<<str<<endl;
//formattedFileObj<<str;
int pos = str.find(">");
int pos3;
while(pos != string::npos)
{
pos3 = str.find("<",pos);
if(str.length() >= pos3+1)
{
if(str.at(pos3+1) == '/')
{
pos = str.find(">",pos3);
}
}
cout<<str.substr(0,pos+1)<<endl;
//formattedFileObj<<str.substr(0,pos+1)<<endl;
str = str.substr(pos+1,string::npos);
pos = str.find(">");
}
}
inputfileObj.close();
//formattedFileObj.close();
}
else
cout<<"could not open file";
}
msvcr90d.dll_写入锁(int fh=14548992,const void*buf=0x77004cc0,unsigned int cnt=4074376)行335+0x3c字节C
ffffffff()
当我暂停执行时,它总是在一个名为write.c的文件和以下代码中停止:
/* write the lf buf and update total */
if ( WriteFile( (HANDLE)_osfhnd(fh),
lfbuf,
(int)(q - lfbuf),
(LPDWORD)&written,
NULL) )
{
charcount += written;
if (written < q - lfbuf)
break;
}
/*写入lf buf并更新总数*/
如果(写入文件((句柄)_osfhnd(fh),
lfbuf,
(int)(q-lfbuf),,
(LPDWORD)和书面文件,
空)
{
字符数+=已写入;
如果(写入
任何人都有可能知道原因是什么,为什么它总是发生在大型未格式化文件中。这一行:
pos = str.find(">",pos3);
如果pos==string::npos,则继续执行以下操作:
str = str.substr(pos+1,string::npos);
pos = str.find(">");
字符串::npos==-1,所以pos+1==0,所以str.substr返回所有str。您现在处于一个无限循环中。我敢打赌当write==0和
q==lbuf
pos=str.find(“>”)时会发生这种情况
再次从字符串的开头开始,您可能希望从您找到的位置继续搜索。是的,peter。。。我的逻辑是这样的:autos sehe+&writed 0x7ffdcff0 int*fh 3866624 int+lfbuf 0x7ffdbbb0 char[5120]的值,我建议在调试器中启动它,一旦它看起来无限循环,(可能很快)暂停它。现在进行迭代,注意关键变量的值。您期望从循环的一次迭代到下一次迭代,事情会发生变化。看看违反了什么期望以及为什么。
str = str.substr(pos+1,string::npos);
pos = str.find(">");