C++ 增压管柱更换不';不要用字符串替换换行符
我正在为我的libspellcheck拼写检查库创建一个函数,用于检查文件的拼写。它的功能是读取文本文件并将其内容发送到拼写检查功能。为了让拼写检查功能正确处理文本,必须用空格替换所有换行符。我决定用boost来做这个。以下是我的功能:C++ 增压管柱更换不';不要用字符串替换换行符,c++,string,boost,C++,String,Boost,我正在为我的libspellcheck拼写检查库创建一个函数,用于检查文件的拼写。它的功能是读取文本文件并将其内容发送到拼写检查功能。为了让拼写检查功能正确处理文本,必须用空格替换所有换行符。我决定用boost来做这个。以下是我的功能: spelling check_spelling_file(char *filename, char *dict, string sepChar) { string line; string fileContents = ""; ifs
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;
}
输出为:
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
正如您所看到的,换行符没有被替换。我做错了什么?
std::getline
从流中提取时不读取换行符,因此它们在文件内容中更新
此外,您不需要搜索和替换“\r\n”
,流将其抽象出来并将其转换为'\n'
,从流中提取换行符,但不将其包含在返回的std::string
中,因此文件内容中没有要替换的换行符
此外,请立即检查输入操作的结果(请参阅):
或者,要将文件内容读入std::string
,请参阅并应用boost::replace_all()
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
while (getline (fileCheck,line))
{
fileContents += line;
}