我正在爲我的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
正如你所看到的,換行不被取代。我究竟做錯了什麼?
胡,jrok打你30秒... –
皮尤pew。無論如何。 – jrok