Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 增压管柱更换不';不要用字符串替换换行符_C++_String_Boost - Fatal编程技术网

C++ 增压管柱更换不';不要用字符串替换换行符

C++ 增压管柱更换不';不要用字符串替换换行符,c++,string,boost,C++,String,Boost,我正在为我的libspellcheck拼写检查库创建一个函数,用于检查文件的拼写。它的功能是读取文本文件并将其内容发送到拼写检查功能。为了让拼写检查功能正确处理文本,必须用空格替换所有换行符。我决定用boost来做这个。以下是我的功能: spelling check_spelling_file(char *filename, char *dict, string sepChar) { string line; string fileContents = ""; ifs

我正在为我的libspellcheck拼写检查库创建一个函数,用于检查文件的拼写。它的功能是读取文本文件并将其内容发送到拼写检查功能。为了让拼写检查功能正确处理文本,必须用空格替换所有换行符。我决定用boost来做这个。以下是我的功能:

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