C++ 在文本文件中搜索和替换

C++ 在文本文件中搜索和替换,c++,search,replace,fstream,C++,Search,Replace,Fstream,我试图在一个文本文件中搜索和替换,但它不起作用,我知道这很简单,就像我错过了一些小东西。我想用一个非空格字符来代替Electric。有人能帮我吗 谢谢 #include <string> #include <fstream> #include <iostream> using namespace std; int main() { string line; ifstream myfile("test.txt"); i

我试图在一个文本文件中搜索和替换,但它不起作用,我知道这很简单,就像我错过了一些小东西。我想用一个非空格字符来代替Electric。有人能帮我吗

谢谢

#include <string>

#include <fstream>
#include <iostream>

using namespace std; 

    int main() {

    string line; 
    ifstream myfile("test.txt");
    if (!myfile.is_open())
    {
        cout << "cant open";
        return 1;
    }
    ofstream myfile2("outfile.txt");

    std::string str("");
    std::string str2("Electric");
    while (getline(myfile, line))
    {
        std::size_t found = str.find(str2);

        found = str.find("Electric");
        if (found != std::string::npos) {
            str.replace(str.find(str2), str2.length(), "");
            myfile2 << found << "\n";
            //std::cout << line << "\n";
        }

        //myfile2 << str2 << "\n";
    }
        remove("test.txt");
        rename("outfile.txt", "test.txt");
        return 0; 
}
#包括
#包括
#包括
使用名称空间std;
int main(){
弦线;
ifstream myfile(“test.txt”);
如果(!myfile.is_open())
{
cout
intmain()
{
使用名称空间std;
ifstream是(“in.txt”);
如果(!is)
返回-1;
流操作系统(“out.txt”);
如果(!os)
返回-2;
字符串f=“电动”;
字符串r=“”;
字符串s;
while(getline(is,s))//从is读取
{
//替换一个:
//std::size\u t p=s.find(f);
//if(p!=std::string::npos)
//s.替换(p,f.长度(),“”);
//替换所有:
for(size\u t p=s.find(f);p!=string::npos;p=s.find(f,p))
s、 替换(p,f.长度(),r);
osA粗溶液:

#ifndef _STREAMBUF_H
#include <streambuf>
#endif


void ReplaceInFile(string inputfile, string outputfile, string replace_what, string replace_with) {

   //Read file into string
    ifstream ifs(inputfile);
    string inputtext((istreambuf_iterator<char>(ifs)),
        istreambuf_iterator<char>());
    ifs.close();


    //Replace string
    size_t replace_count = 0;
    size_t found = !string::npos /*not npos*/;
    while (!(found == string::npos)) {
        found = inputtext.find(replace_what, found);
        if (found != string::npos) {
            ++replace_count;
            inputtext.replace(found, replace_what.length(), replace_with);
        }
    }

    //Output how many replacements were made
    cout << "Replaced " << replace_count, (replace_count == 1) ? cout << " word.\n" : cout << " words.\n";

    //Update file
    ofstream ofs(outputfile /*,ios::trunc*/); //ios::trunc with clear file first, else use ios::app, to append!!
    ofs << inputtext; //output now-updated string
    ofs.close();
}

什么(如何)不起作用?例如,我创建的文本文件中有一个完整的段落,但我只想删除字符串“Electric”但当我将程序运行到文本文件时,实际上什么都没有发生。@bjarnestroutrupare你想修改这个文件吗?你能指出你要修改它的行吗?str.replace(str.find(str2),str2.length(),“”);…修改它不是为了用“”替换单词吗?@ZDFThanks谢谢你的帮助,我真的很感激。我不知道是不是因为我不明白,但我最终用你所做的替换了它,并将我的代码放入你创建的内容中,但仍然不起作用。@ZDFSee更新的答案。我刚刚清理并复制了你的代码。请注意,你的代码每行只删除一个外观。请参阅更新的answer、 我为replaceall添加了代码。
#ifndef _STREAMBUF_H
#include <streambuf>
#endif


void ReplaceInFile(string inputfile, string outputfile, string replace_what, string replace_with) {

   //Read file into string
    ifstream ifs(inputfile);
    string inputtext((istreambuf_iterator<char>(ifs)),
        istreambuf_iterator<char>());
    ifs.close();


    //Replace string
    size_t replace_count = 0;
    size_t found = !string::npos /*not npos*/;
    while (!(found == string::npos)) {
        found = inputtext.find(replace_what, found);
        if (found != string::npos) {
            ++replace_count;
            inputtext.replace(found, replace_what.length(), replace_with);
        }
    }

    //Output how many replacements were made
    cout << "Replaced " << replace_count, (replace_count == 1) ? cout << " word.\n" : cout << " words.\n";

    //Update file
    ofstream ofs(outputfile /*,ios::trunc*/); //ios::trunc with clear file first, else use ios::app, to append!!
    ofs << inputtext; //output now-updated string
    ofs.close();
}
ReplaceInFile("tester.txt", "updated.txt", "foo", "bar");