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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34507989/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 14:23:31  来源:igfitidea点击:

update and delete data from file in c++

c++

提问by El-Sayed Khaled

How can I update data from file in c++ without display the old data? I want to delete specific data and update. For example I want to update the name only and another time update gpa only with delete the old gpa?

c++ - 如何在不显示旧数据的情况下更新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;
}

回答by roottraveller

First you have to copy all the other records to a temporary file eg. temp.txtand then delete the old file data.txtand rename temp.txtto data.txt. Now write the new record to file data.txt.

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

code:

代码:

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

回答by Sun Dro

Line by line read old file and copy lines to a new file. When you find the line which you want to update, change it with your line and copy it to the new file. When you reach EOF, delete old file and rename new file with the name of old file.

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

It will be something like that:

它会是这样的:

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