在C ++中搜索并替换txt文件中的字符串 [英] Search and replace string in txt file in c++

查看:297
本文介绍了在C ++中搜索并替换txt文件中的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在文件中找到一个字符串并将其替换为用户输入.
这是我的粗略代码.

I want to find a string in a file and replace it with user input.
Here is my rough code.

#include <iostream>
#include <fstream.h>
#include <string.h>

int main(){
    istream readFile("test.txt");

    string readout,
         search,
         replace;

    while(getline(readFile,readout)){
        if(readout == search){
            // How do I replace `readout` with `replace`?
        }
    }
}

更新
这是解决我的问题的代码

UPDATE
Here is the code that solved my problem

test.txt:

id_1
arfan
haider

id_2
saleem
haider

id_3
someone
otherone

C ++代码:

#include <iostream>
#include <fstream>
#include <string>

using namesapce std;

int main(){
    istream readFile("test.txt");
    string readout,
           search,
           firstname,
           lastname;

    cout << "Enter the id which you want to modify";
    cin >> search;

    while(getline(readFile,readout)){
        if(readout == search){
            /*
                id remains the same
                But the First name and Last name are replaced with
                the user `firstname` and `lastname` input
            */
            cout << "Enter new First name";
            cin >> firstname;

            cout << "Enter Last name";
            cin >> lastname;  
        }
    }
}  

假设:
用户搜索ID id_2.在该用户之后输入名字和姓氏ShafiqAhmed. 运行此代码后,test.txt文件必须按如下所示修改记录:

Suppose:
A user searches for id id_2. After that user enter First name and Last name Shafiq and Ahmed. After runing this code the test.txt File must modify the record like that:

…

id_2
Shafiq
Ahmad

…

id_2记录更改,其余文件将保持不变.

Only the id_2 record changes, the remaining file will stay the same.

推荐答案

这应该有效.我使用string::find在每一行中找到所需的子字符串,并使用string::replace替换了发现的内容.

This should work. I used string::find to find the desired substring within each line, and string::replace to replace it if something has been found.

编辑:我忘记了单词每行出现多次的情况.添加了while来解决此问题.

I forgot about the case where the word occurs multiple times per line. Added a while to fix this.

#include <fstream>
#include <iostream>
using namespace std;

int main(int argc, char **argv)
{
    ifstream in(argv[1]);
    ofstream out(argv[2]);
    string wordToReplace(argv[3]);
    string wordToReplaceWith(argv[4]);

    if (!in)
    {
        cerr << "Could not open " << argv[1] << "\n";
        return 1;
    }

    if (!out)
    {
        cerr << "Could not open " << argv[2] << "\n";
        return 1;
    }

    string line;
    size_t len = wordToReplace.length();
    while (getline(in, line))
    {
        while (true)
        {
            size_t pos = line.find(wordToReplace);
            if (pos != string::npos)
                line.replace(pos, len, wordToReplaceWith);
            else 
                break;
        }

        out << line << '\n';
    }
}

这篇关于在C ++中搜索并替换txt文件中的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆