在c ++中搜索和替换txt文件中的字符串

Axe*_*eem 5 c++ search replace file

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

#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`?
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

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

的test.txt:

id_1
arfan
haider

id_2
saleem
haider

id_3
someone
otherone
Run Code Online (Sandbox Code Playgroud)

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;  
        }
    }
}  
Run Code Online (Sandbox Code Playgroud)

假设:
用户搜索id id_2.该用户输入后,首先名和姓ShafiqAhmed.运行此代码后,test.txtFile必须修改如下记录:

…

id_2
Shafiq
Ahmad

…
Run Code Online (Sandbox Code Playgroud)

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

Avi*_*vet 5

我会做@stefaanv所说的:

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

int main(){
  ostream outFile("replaced.txt");
  istream readFile("test.txt");
  string readout;
  string search;
  string replace;
  while(getline(readFile,readout)){
    if(readout == search){
      outFile << replace;
    }
    else {
      outFile << readout;
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

编辑:如果每行的信息独立于其他行的信息,则上述解决方案有效.在您的更新中,名称行上的信息取决于id行的信息.因此,为了扩展上述技术,您需要在while循环中维护状态,以指示何时到达一个数据块的末尾.

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

int main(){
  ostream outFile("replaced.txt");
  istream readFile("test.txt");
  string readout;
  string search, Fname, Lname;
  unsigned int skipLines = 0;

  cout << "Enter id which you want Modify";
  cin >> search;
  cout << "Enter new First name";
  cin >> Fname;
  cout << "Enter Last name";
  cin >> Lname;  

  while(getline(readFile,readout)) {
    if (skipLines != 0) {
      skipLines--;
      continue;
    }
    else if (readout == search) {
      outFile << search << endl;
      outFile << Fname << endl;
      outFile << Lname << endl;
      skipLines = 2;
    }
    else {
      outFile << readout;
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

稍微优雅的方法是将每个数据块存储在结构中,这允许您使用重载的运算符<<&>>.这使得文件读写的代码更加清晰 - 它实际上与"每行上的数据是独立的"代码的情况相同.

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

struct NameRecord {
  string id;
  string fname;
  string lname;

  friend std::ostream& operator<<(std::ostream &os, const NameRecord &src);
  friend std::istream& operator>>(std::istream &is, NameRecord &dst);
};

std::ostream& operator <<(std::ostream &os, const NameRecord &src) {
  os << src.id << endl << src.fname << endl << src.lname << endl;

  return os;
}

std::istream& operator >>(std::istream &is, NameRecord &dst) {
  // may need to have more code to ignore whitespace, I'm not sure
  if (is.good ()) {
    is >> dst.id;
  }
  if (is.good ()) {
    is >> dst.fname;
  }
  if (is.good ()) {
    is >> dst.lname;
  }

  return is;
}

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

  NameRecord inRecord, replaceRecord;
  cout << "Enter id which you want Modify";
  cin >> replaceRecord.id;
  cout << "Enter new First name";
  cin >> replaceRecord.Fname;
  cout << "Enter Last name";
  cin >> replaceRecord.Lname;  

  while (readFile.good()) {
    // the >> operator reads the whole record (id, fname, lname)
    readFile >> inRecord;

    // the << operator writes the whole record
    if (inRecord.id == replaceRecord.id) {
      outFile << replaceRecord;
    }
    else {
      outFile << inRecord;
    }
  }
} 
Run Code Online (Sandbox Code Playgroud)


Jor*_*eit 5

这应该工作。我过去常常string::find在每一行中找到所需的子字符串,string::replace如果发现了某些东西就将其替换。

编辑:我忘了单词每行出现多次的情况。添加了一个while解决此问题的方法。

#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';
    }
}
Run Code Online (Sandbox Code Playgroud)