2017-10-06 1 views
0

파일을 열고 한 줄에 키워드를 검색했습니다. 해당 키워드가 발견되면 파일에서 동일한 키워드를 다시 찾을 때까지 계속 파일을 읽습니다. 대부분의 경우 작동합니다. 그러나 한 경우 정확하게 작동하지 않습니다.이 줄getline이 파일을 열지 않고 작동하지 않습니다.

::MessageBox(NULL, TEXT("here -2"), TEXT(""), MB_OK); 

은 내가 확인했을 때 절대로 실행되지 않습니다. 나는 findline.getline() 함수를 의심하고있다. 내가 찾고있는 텍스트는 파일에 있으며 공백이있는 문자열 [AB CD EF "]와 같습니다.이 파일은 행의 첫 번째 패턴으로 존재하며이 텍스트 앞에 공백이 없습니다. 누구나 내가 무슨 실수를 할 수 있습니까? C++에서 새로운 것입니다. 여기 내 코드가있다.

std::ifstream in(curr_file_path); 
std::string search("SEARCH"); 
std::string line; 

char *pDestText = new char[8000*256+1]; 
int f_match = -1; 
int l_match = -1; 
int r_val=0; 
int textLen = 0; 
int flag = 0; 
    while (std::getline(in, line)) 
    { 

     r_val = line.find(search); 
     if (r_val!=(std::string::npos)) 
     { 
      ::MessageBox(NULL, TEXT("here -2"), TEXT(""), MB_OK); 
      f_match = r_val; 

      while (std::getline(in, line)) 
      { 
       ::MessageBox(NULL, TEXT("here -3"), TEXT(""), MB_OK); 
       r_val = line.find(search); 
       if (r_val != std::string::npos) 
       { 
        l_match = r_val; 
        break; 
       } 
       else 
       { 
        ::MessageBox(NULL, TEXT("here -4"), TEXT(""), MB_OK); 
        for (int i = 0; i < line.size(); i++) 
        { 
         pDestText[textLen++] = line[i]; 
        } 
       } 
      } 
      ::MessageBox(NULL, TEXT("here -5"), TEXT(""), MB_OK); 
      for (int i = 0; i < r_val; i++) 
      { 
       //if(line[i]!='=') 
       ::MessageBox(NULL, TEXT("here -6"), TEXT(""), MB_OK); 
       pDestText[textLen++] = line[i]; 
      } 
      //pDestText[textLen] = '\0'; 
      ::MessageBox(NULL, TEXT("here -7"), TEXT(""), MB_OK); 
      break; 
     } 
    } 
    in.close(); 
+3

디버거를 사용하여 파일을 읽고 파싱 할 때 한 번에 한 줄씩 코드를 단계별로 실행할 때 여기에 표시된 모든 변수는 각 단계에서 해당 코드가 검색 문자열을 포함하는 행을 읽을 때 어떤 관측을 했습니까? 앉아서 5 분 안에 자신을 알아낼 수있을 때 stackoverflow.com에서 누군가를 알아낼 때까지 기다릴 이유가 없습니다. 디버거를 사용하는 법을 배워야합니다. 디버거를 사용하는 방법을 아는 것은 모든 C++ 개발자에게 필수적인 기술입니다. 이것이 바로 그 때문입니다. –

+2

샘이 100 % 맞습니다! 그리고 디버거를 사용하지 않거나 사용하지 않는다면 적어도 디버그 프린트를 사용하십시오. 예를 들어 getline이 작동하지 않는다고 말하면'std :: cout << line << std :: endl '읽은 것을보고 나서 ... –

답변

0

나는 깨끗한 방법으로 사람들에게 표준 C++을 보여주고 싶습니다 .¹.

#include <fstream> 
#include <string> 
#include <iostream> 
#include <iterator> 
#include <sstream> 

// start SEARCH block 

bool filter_lines(std::istream& haystack, std::string const& needle, std::ostream& dest) { 
    bool active = false; 
    std::string line; 

    auto trigger = [&] { 
     bool match = std::string::npos != line.find(needle); 
     if (match) 
      active = !active; 
     return match; 
    }; 

    auto act = [&] { if (active) dest << line << "\n"; }; 

    while (getline(haystack, line)) 
     if (!trigger()) 
      act(); 

    return active; 
} 

int main() { 
    std::ifstream ifs("main.cpp"); 
    std::stringstream destText; 

    if (filter_lines(ifs, "SEARCH", destText)) 
     std::cerr << "Warning: last delimited block unclosed\n"; 

    std::cout << "------------- FILTERED -----------\n" 
       << destText.rdbuf() 
       << "----------------------------------\n"; 
} 

Live On Coliru

은 내가 정확한 문제를 해결하지 않을 수 있습니다 실현 ¹,하지만 난 대답이 유형은 종종 더 유리하다 생각합니다. 디버거를 사용하여 실수를 찾아내는 구성적인 설명도 마찬가지입니다.

관련 문제