2013-08-02 2 views
2

파일의 맞춤법을 검사하는 libspellcheck 맞춤법 검사 라이브러리에 대한 함수를 만들고 있습니다. 함수는 텍스트 파일을 읽고 그 내용을 맞춤법 검사 기능으로 보냅니다. 맞춤법 검사 기능으로 텍스트를 올바르게 처리하려면 모든 줄 바꿈을 공백으로 바꿔야합니다. 나는 이것을 위해 부스트를 사용하기로 결정했다. 여기 내 기능은 다음과 같습니다.부스트 문자열 바꾸기가 줄 바꿈을 문자열로 바꾸지 않는다

spelling check_spelling_file(char *filename, char *dict, string sepChar) 
{ 

    string line; 
    string fileContents = ""; 
    ifstream fileCheck (filename); 
    if (fileCheck.is_open()) 
    { 
     while (fileCheck.good()) 
      { 
       getline (fileCheck,line); 
      fileContents = fileContents + line; 
     } 

     fileCheck.close(); 
    } 
    else 
    { 
     throw 1; 
    } 

    boost::replace_all(fileContents, "\r\n", " "); 
    boost::replace_all(fileContents, "\n", " "); 

    cout << fileContents; 

    spelling s; 
    s = check_spelling_string(dict, fileContents, sepChar); 

    return s; 
} 

라이브러리를 컴파일 한 후 샘플 파일을 사용하여 테스트 응용 프로그램을 만들었습니다.

테스트 응용 프로그램 코드 :

#include "spellcheck.h" 

using namespace std; 

int main(void) 
{ 
    spelling s; 
    s = check_spelling_file("test", "english.dict", "\n"); 

    cout << "Misspelled words:" << endl << endl; 
    cout << s.badList; 
    cout << endl; 

    return 0; 
} 

테스트 파일 :

This is a tst of the new featurs in this library. 
I wonder, iz this spelled correcty. 

출력은 : 당신이 볼 수 있듯이

This is a tst of the new featurs in this library.I wonder, iz this spelled correcty.Misspelled words: 

This 
a 
tst 
featurs 
libraryI 
iz 
correcty 

의 줄 바꿈은 교체되지 않습니다. 내가 도대체 ​​뭘 잘못하고있는 겁니까?

답변

4

std::getline()은 스트림에서 개행 문자를 추출하지만 반환되는 std::string에는 포함하지 않으므로 대체 할 fileContents에 개행 문자가 없습니다.

또한, 입력 조작의 검사 결과가 즉시 (Why is iostream::eof inside a loop condition considered wrong? 참조)

대안
while (getline (fileCheck,line)) 
{ 
    fileContents += line; 
} 

는하는 std::string으로 파일의 내용을 읽어 What is the best way to read an entire file into a std::string in C++? 참조하고 boost::replace_all()를 적용.

+0

아, 조크가 30 초 동안 너를 이길 ... –

+1

퓨 리. 어쨌든 +1하십시오. – jrok

5

std::getline 스트림에서 추출 할 때 줄 바꿈 문자를 읽지 않으므로 fileContents에 쓰여집니다.

또한 "\r\n"을 검색하고 바꿀 필요가 없습니다. 스트림을 추상화하여이 파일을 '\n'으로 변환하십시오.

+0

:) 그리고 +1! – hmjd

관련 문제