2014-01-17 150 views
3

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

#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
这里是解决我的问题代码

的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​​。在那之后,用户输入名字和姓氏ShafiqAhmed。 乳宁这个代码test.txt文件必须修改记录一样,后:

… 

id_2 
Shafiq 
Ahmad 

… 

只有​​记录更改,剩下的文件将保持不变。

+0

建议:写入一个新文件并将其移动到原始文件+ std :: string中查找并替换成员函数。 – stefaanv

+0

我刚才看到你想要替换完整的行,所以只有我的第一个建议是有效的。第二个是取代部分线条。文件中的内联替换仅适用于替换相同的大小。 – stefaanv

回答

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

编辑:上述解决方案的工作,如果每一行的信息是独立于其他行的信息。在更新中,名称行上的信息取决于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; 
    } 
    } 
} 

稍微更优雅的方式将每个数据块存储在一个结构,它允许您使用重载运算< < & >>。这使得读取文件的代码更加清晰 - 它与“每行上的数据是独立的”情况的代码基本相同。

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

这应该有效。我使用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'; 
    } 
}