2012-03-30 3 views
0

나는 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()); 

    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"; 
} 

}

을하지만,하지만, 구글의 홈 페이지 소스와 같은 큰 HTML 파일 것이 퓐 작동 작은 파일이 기능을 사용하는 경우는 무한 루프로 이동합니다.

다음은 호출 스택입니다.

ntdll.dll!76f99a94()  
[Frames below may be incorrect and/or missing, no symbols loaded for ntdll.dll] 
ntdll.dll!76f98d94()  
ntdll.dll!76fa9522()  
kernel32.dll!7588cb6c()  
kernel32.dll!7588cbfc()  
kernel32.dll!7588c964()  

msvcr90d.dll! _write_nolock (INT의 FH = 14,548,992, CONST 보이드 * BUF = 0x77004cc0, 부호 INT CNT = 4,074,376) 라인 (335) + 0x3c 바이트 C FFFFFFFF()

그리고

   /* 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; 
       } 

이유가 될 수있는 어떤 단서를 가지고 어느 하나, 왜 항상 하 : 난 실행을 일시 정지 할 때 항상 write.c라는 하나 개의 파일과 다음 코드에서 정지 큰 포맷되지 않은 파일이있는 ppens.

+0

나는 == 0과'q == lbuf'라고 쓰면 기꺼이하겠다. – sehe

+1

'pos = str.find (">");'다시'string'의 시작 부분에서 다시 시작한다. 어디서 왔는지 계속 검색하고 싶습니다. –

+0

네 피터는 ... 내 논리는 간다 그런 식으로 만 –

답변

1

이 줄 :

pos = str.find(">",pos3); 

문자열 : 비영리 단체가, 다음에 수행 == pos가이 작업을 수행하는 경우 :

str = str.substr(pos+1,string::npos); 
pos = str.find(">"); 

문자열 :: 비영리 단체 == -1, POS 그래서 + 1 == 0이므로 str.substr은 모든 str을 반환합니다. 당신은 이제 무한 루프에 있습니다.

관련 문제