Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/160.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.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++_Arrays_File - Fatal编程技术网

C++ 用文件中的另一个单词替换特定单词

C++ 用文件中的另一个单词替换特定单词,c++,arrays,file,C++,Arrays,File,如何在文本文件中找到一个特定单词,并用另一个单词替换该单词,然后将测试写回文件(如果文本以段落分隔);如果其段落未分隔,请使用string.find和string replace 据我所知,您对在一个段落中替换单词感到满意,并且对多个段落的文本表示怀疑 请查看名为“getline()”函数的函数 此函数读取整个文本,直到遇到“\n”元素(下一行) 因此,您可以使用这个getline函数将整个段落转换为字符串 在while循环中使用这个getline函数可以从文本文件中获取所有段落 下面提供了一个

如何在文本文件中找到一个特定单词,并用另一个单词替换该单词,然后将测试写回文件(如果文本以段落分隔);如果其段落未分隔,请使用string.find和string replace

据我所知,您对在一个段落中替换单词感到满意,并且对多个段落的文本表示怀疑

请查看名为“getline()”函数的函数

此函数读取整个文本,直到遇到“\n”元素(下一行)

因此,您可以使用这个getline函数将整个段落转换为字符串

在while循环中使用这个getline函数可以从文本文件中获取所有段落

下面提供了一个示例代码

#include<iostream>
#include<string>
#include<fstream>
using namespace std;
int main()
{
    string a,b;
    a="he";
    b="she";
    fstream text("text.txt");
    string line;
    while (!text.eof( ))
    {
        getline(text,line);
        cout<<line<<endl;
        //This string "line" is basically a string containing your first paragraph
        //ADD your find and replace code here for the string "line".
        //The second time the while loop executes the string "line" will contain the second paragraph and so on..
    }
}
}
#包括
#包括
#包括
使用名称空间std;
int main()
{
a、b串;
a=“他”;
b=“她”;
fstream文本(“text.txt”);
弦线;
而(!text.eof())
{
getline(文本,行);

将文件放入某种容器中(我建议
std::vector
),然后仔细查看。定义段落分隔的含义?如果搜索词位于下一段的开头。我的代码没有检测到该词。这是我的代码。while(!(fid.eof()){getline(fid,data,”);if(searchKey.compare(data)==0{count=count+1;}段落以“\n”结尾对。段落以“\n”结尾,这是下一行转义序列。因此,当您读取文件时,您会将两段合并为一个连接到\n的段落。您可以将其可视化为段落1\n段落2。这意味着getline函数在段落2的开头包含带word的字符。如果您在第二段开始时使用空格,您将得到第一段的最后一句话也会遇到同样的问题。我已经试过了。谢谢。。。。