c++ - 在 C++ 中查找和修改 txt 文件

标签 c++ file search replace

我想从文件中找到一个特定的id并修改内容。
这是我想要的原始代码。

// 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;
     string search;
     string Fname;
     string Lname;

     cout<<"Enter id which you want Modify";
     cin>>search;

     while(getline(readFile,readout)){
        if(readout == search){
           /*
                id remain same (does not change)
                But First name and Last name replace with
                user Fname and Lname
           */
            cout<<"Enter new First name";
                 cin>>Fname;
            cout<<"Enter Last name";
                 cin>>Lname;  
        }
    }
}  

假设:
用户搜索 ID *id_2*。在该用户输入名字和姓氏 ShafiqAhmed 之后。
运行此代码后,test.txt 文件必须像这样修改记录:

...............
...............

id_2
Shafiq
Ahmad

.................
.................

只有 id_2 记录更改剩余文件将是相同的。
更新:

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

using namespace std;

int main()
{
ofstream outFile("temp.txt");
ifstream readFile("test.txt");

string readLine;
string search;
string firstName;
string lastName;

cout<<"Enter The Id :: ";
cin>>search;

while(getline(readFile,readLine))
{
    if(readLine == search)
    {
        outFile<<readLine;
        outFile<<endl;

        cout<<"Enter New First Name :: ";
        cin>>firstName;
        cout<<"Enter New Last Name :: ";
        cin>>lastName;

        outFile<<firstName<<endl;
        outFile<<lastName<<endl;
    }else{
        outFile<<readLine<<endl;
    }
}
}

它还在 temp.txt 文件中包含之前的 First Name 和 Last Name。

最佳答案

找到具体的id并写入新的名字和姓氏后,需要跳过下面两行。此代码有效:

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

using namespace std;
void skipLines(ifstream& stream, int nLines)
{
    string dummyLine;
    for(int i = 0; i < nLines; ++i)
        getline(stream, dummyLine);
}

int main()
{
    ofstream outFile("temp.txt");
    ifstream readFile("test.txt");

    string readLine;
    string search;
    string firstName;
    string lastName;

    cout<<"Enter The Id :: ";
    cin>>search;

    while(getline(readFile,readLine))
    {
        if(readLine == search)
        {
            outFile<<readLine;
            outFile<<endl;

            cout<<"Enter New First Name :: ";
            cin>>firstName;
            cout<<"Enter New Last Name :: ";
            cin>>lastName;

            outFile<<firstName<<endl;
            outFile<<lastName<<endl;
            skipLines(readFile, 2);
        }
        else
        {
            outFile<<readLine<<endl;
        }
    }
}

关于c++ - 在 C++ 中查找和修改 txt 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21212678/

相关文章:

mysql - 需要通过多个字段搜索的技巧[mysql优化]

c# - 为什么 C 和 C++ 中的 float / double 没有除余运算?

C++双向链表 - 使用pop_back()从尾部删除元素

java - 如何将二维字符数组写入文件

计算给定文本文件的字数

regex - 在字符串中搜索/替换

c++ - 使用 *void 作为 static_cast 的缓冲区

c++ - 来自 C++ boost 线程的 NSOpenPanel

c++ - 仅使用 C/C++ API 写入期间的 iOS 文件大小

php - 从 mySQL 查询中删除未找到的单词