2014-01-17 152 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'; 
    } 
}