在c++中更新和删除文件中的数据

update and delete data from file in c++

本文关键字:文件 删除 数据 c++ 更新      更新时间:2023-10-16

如何在不显示旧数据的情况下从c++中的文件更新数据?我想删除特定的数据并更新。例如,我只想更新名称,另一次只更新gpa,删除旧的gpa?

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
    int k=0;
    string line;
    string find;
    char name[25];
    int id=0; 
    float gpa=0;
    ofstream myfile;
    myfile.open("data.txt");
    while(k!=3){
        cout<<"press 1 for adding data"<<endl;
        cout<<"press 2 for update "<<endl;
        cin>>k;
        if(k==1)
        { 
            cout<<"enter ID "<<endl;
            cin>>id;
            cout<<"enter Name"<<endl;
            cin>>name;
            cout<<"enter GPA "<<endl;
            cin>>gpa;
            myfile<<name<<endl;
            myfile<<id<<endl;
            myfile<<gpa<<endl<<endl<<endl;
        }
        if(k==2)
        {
            cout<<"name u want to update "<<endl;
            cin>>find; 
            ifstream file;
            file.open("data.txt");
            while (!file.eof() && line!=find)
            {
                getline(file,line);
            }
            cout<<"enter ID "<<endl;
            cin>>id;
            cout<<"enter Name"<<endl;
            cin>>name;
            cout<<"enter GPA "<<endl;
            cin>>gpa;
            myfile<<name<<endl;
            myfile<<id<<endl;
            myfile<<gpa<<endl<<endl<<endl;
        }
        if(k==3){
            myfile.close();
        }
    }
    return 0;
}

首先,您必须将所有其他记录复制到一个临时文件中,例如temp.txt,然后删除旧文件data.txt,并将temp.txt重命名为data.txt。现在将新记录写入文件data.txt

代码:

if(k==2)
 {
    cout<<"name u want to update "<<endl;
    cin>>find;
    ifstream myfile2; //for reading records
   myfile2.open("data.txt");
    ofstream temp;
    temp.open("temp.txt");
    while (getline(myfile2, line))
    {
      if (line != find)
        temp << line << endl;
    }
    myfile2.close();
    temp.close();
    remove("data.txt");
    rename("temp.txt", "data.txt");
    //Now add new Record to file
    cout<<"enter ID "<<endl;
    cin>>id;
    cout<<"enter Name"<<endl;
    cin>>name;
    cout<<"enter GPA "<<endl;
    cin>>gpa;
    ofstream myfile;
    myfile.open("data.txt", ios::app | ios::out);
    myfile<<name<<endl;
    myfile<<id<<endl;
    myfile<<gpa<<endl<<endl<<endl;
}

逐行读取旧文件并将行复制到新文件中。找到要更新的行后,使用行对其进行更改,并将其复制到新文件中。当您到达EOF时,删除旧文件并用旧文件的名称重命名新文件。

它将是这样的:

int replace_line(const char *fname, const char *oldline, const char* newline)
{
    int done = 0;
    /* Open templorary file */
    FILE *newfp = fopen("file.tmp", "a");
    if (newfp != NULL) 
    {
        /* Open exiting file */
        FILE *oldfp = fopen(fname, "r");
        if(oldfp != NULL)
        {
            ssize_t read;
            size_t len = 0;
            char * line = NULL;
            /* Line-by-line read from exiting file */            
            while ((read = getline(&line, &len, oldfp)) != -1) 
            {
                if(strstr(line, oldline) == NULL) fprintf(newfp, "%s", line);
                else fprintf(newfp, "%s", newline);
            }
            /* Clean up memory */
            fclose(oldfp);
            if (line) free(line);
            done = 1;
        }
        fclose(newfp);
        remove(fname);
        rename("file.tmp", fname);
        return done;
    }
    return 0;
}