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

时间:2015-12-29 09:17:58

标签: 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;
}

2 个答案:

答案 0 :(得分:2)

首先,您必须将所有其他记录复制到临时文件,例如。 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;
}

答案 1 :(得分:2)

逐行读取旧文件并将行复制到新文件。当您找到要更新的行时,请使用您的行进行更改并将其复制到新文件中。当您到达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;
}