dimanche 3 janvier 2021

How to delete and edit a line in text file c++

Hello I am beginner in C++. I programmed a code for deleting and editing time of movies in text file. But I have stucked there. The program is deleting all the data in the file. Can someone help me with this? I have attached the arrangement of movie just like how I arranged in text file which is "Movies.txt"

Avenger|09:30|08:00|07:00

Policeman|09:30|08:00|07:00

Superman|09:30|08:00|07:00

Batman|09:30|08:00|07:00

delete_movie()
{   
    char name [25];
    char name_temp [25];

    char time1[25];
    char time2[25];
    char time3[25];
    char time1_temp[25];
    char time2_temp[25];
    char time3_temp[25];
    int response_delete;
    fstream movie;
    movie.open("Movies.txt",ios::in);
 
cout<<"======================================================================================================================" << endl;
    cout<<"|SERIAL NUMBER|               TIME[SHOW1]              TIME[SHOW2]              TIME[SHOW3]            |MOVIE NAME|"    << endl;
    cout<<"======================================================================================================================" << endl;

    int count = 0;
    string line;

    ifstream file("Movies.txt");
    while (getline(file, line))
        count++;

    int i = 0;
    while(!movie.eof(), i++ , i<= count)
    {
    movie.getline(name,25,'|');
    movie.getline(time1,25,'|');
    movie.getline(time2,25,'|');
    movie.getline(time3,25);
    cout<<"    (" << i << ") " << "\t\t\t " << time1 << "\t\t\t " << time2 << "\t\t\t " << time3 << "\t\t\t " <<name << "\n";
    }

    ofstream temp;
    temp.open("temp.txt",ios::out);
    cout << endl;
    cout<<"Enter the serial number of movie you want to delete:  ";
    cin>> response_delete;

    int a = 0;
    while(!movie.eof(), a++ , a <= response_delete)
    {
      movie.getline(name,25,'|');
      movie.getline(time1,25,'|');
      movie.getline(time2,25,'|');
      movie.getline(time3,25);
    }

     string temporary = name;

      while(!movie.eof())
    {
      movie.getline(name,25,'|');
      movie.getline(time1,25,'|');
      movie.getline(time2,25,'|');
      movie.getline(time3,25);
      if(strcmp(name,temporary)==0)
        {
         continue;
        }
        else
        {
         temp<< name_temp <<'|'<< time1_temp <<'|'<< time2_temp <<'|' <<time3_temp <<'\n';
        }
    }
}

update_movie()
{
    char name [25];
    char name_temp [25];
    char time1[25];
    char time2[25];
    char time3[25];
    char time1_temp[25];
    char time2_temp[25];
    char time3_temp[25];

    int response_update;
    fstream movie;
    movie.open("Movies.txt",ios::in);

    cout<<"======================================================================================================================" << endl;
    cout<<"|SERIAL NUMBER|               TIME[SHOW1]              TIME[SHOW2]              TIME[SHOW3]            |MOVIE NAME|" << endl;
    cout<<"======================================================================================================================" << endl;

    int count = 0;
    string line;

    ifstream file("Movies.txt");
    while (getline(file, line))
        count++;
    int i = 0;
    while(!movie.eof(), i++ , i<= count)
    {
    movie.getline(name,25,'|');
    movie.getline(time1,25,'|');
    movie.getline(time2,25,'|');
    movie.getline(time3,25);
    cout<<"    (" << i << ") " << "\t\t\t " << time1 << "\t\t\t " << time2 << "\t\t\t " << time3 << "\t\t\t " <<name << "\n";
    }

    fstream temp;

    movie.open("Movies.txt",ios::in);
    temp.open("temp.txt",ios::out);
    cin.ignore();
    cout<<"Enter the serial number of movie you want to update:  ";
    cin>> response_update;

    int a = 0;
    while(!movie.eof(), a++ , a <= response_update)
    {
      movie.getline(name,25,'|');
      movie.getline(time1,25,'|');
      movie.getline(time2,25,'|');
      movie.getline(time3,25);
    }

    while(!movie.eof())
    {
      movie.getline(name_temp,25,'|');
      movie.getline(time1_temp,25,'|');
      movie.getline(time2_temp,25,'|');
      movie.getline(time3_temp,25);
        if(strcmp(name,name_temp)==0)
        {
            cout<<"Enter the new time for SHOW[1] : ";
            cin.getline(time1,25);
            cout<<"Enter the new time for SHOW[2] : ";
            cin.getline(time2,25);
            cout<<"Enter the new time for SHOW[3] : ";
            cin.getline(time3,25);
            temp << name<<'|'<<time1<<'|'<<time2<<'|'<<time3<<'\n';
        }
        else
        {
            temp << name<<'|'<<time1<<'|'<<time2<<'|'<<time3<<'\n';
        }

    }
    temp.close();
    movie.close();

    movie.open("Movies.txt",ios::out);
    temp.open("temp.txt",ios::in);
    while(!temp.eof())
    {
        temp.getline(name_temp,25,'|');
        temp.getline(time1,25,'|');
        temp.getline(time2,25,'|');
        temp.getline(time3,25);
        movie << name<<'|'<<time1<<'|'<<time2<<'|'<<time3<<'\n';
    }
    temp.close();
    movie.close();
    remove("temp.txt");
    cout<<"\n done !!! \n";
}

Aucun commentaire:

Enregistrer un commentaire