0

How to edit specific string in text file?

I use my logic but it's not working..

Really appreciate anyone help me

Here is my Add Function

void Admin::add_dvd()
{
    string id, copy, title, genre, dir, rentdate, returndate, fee;
    ofstream myfile("dvd_list.txt", ios::out | ios::app);
    cin.get();
    cout << "DVD ID: " << endl;
    getline(cin, id);
    cout << "Copy Number: " << endl;
    getline(cin, copy);
    cout << "Title: " << endl;
    getline(cin, title);
    cout << "Genre: " << endl;
    getline(cin, genre);
    cout << "Director: " << endl;
    getline(cin, dir);
    cout << "Rental Date: " << endl;
    getline(cin, rentdate);
    cout << "Return Date: " << endl;
    getline(cin, returndate);
    cout << "Rental Fee: " << endl;
    getline(cin, fee);
    myfile << endl << id << " | " << copy << " | " << title << " | " << genre << " | " << dir << " | " << rentdate << " | " << returndate << " | " << fee << endl;
    myfile.close();

    cout << "DVD Added." << endl;
    system("pause");

}

This is the EDIT FUNCTION

 void Admin::edit_dvd()
    {
        string id;
        cout << "Enter DVD ID: " << endl;
        cin >> id;
        ifstream myfile("dvd_list.txt");
        if (myfile.is_open())
        {
            while (!myfile.eof())
            {
                myfile >> dvd_id >> title >> genre >> dir >> rent_date >> return_date >> copy_num >> rent_fee;

                if (id == dvd_id)
                {
                    int choice;
                    cout << dvd_id << title << genre << dir << rent_date << return_date << copy_num << rent_fee << endl;
                    cout << "Which part you want to edit? " << endl;
                    cout << "1) DVD ID" << endl;
                    cout << "2) DVD Title" << endl;
                    cout << "3) Genre" << endl;
                    cout << "4) Director" << endl;
                    cout << "5) Rent Date" << endl;
                    cout << "6) Return Date" << endl;
                    cout << "7) Copy Number" << endl;
                    cout << "8) Rent Fee" << endl;
                    cout << "9) Cancel" << endl;
                    cout << "Enter number: " << endl;
                    cin >> choice;
                    switch (choice)
                    {
                    case 1:
                    {
                        string id;
                        cout << "Enter new DVD ID: " << endl;
                        cin >> id;
                        id = dvd_id;
                    }

                    case 2:
                    {
                        string ttl;
                        cout << "Enter new DVD Title: " << endl;
                        cin >> ttl;
                        ttl = title;
                    }

                    case 3:
                    {
                        string gen;
                        cout << "Enter new Genre: " << endl;
                        cin >> gen;
                        gen = genre;
                    }

                    case 4:
                    {
                        string director;
                        cout << "Enter new Director: " << endl;
                        cin >> director;
                        director = dir;
                    }

                    case 5:
                    {
                        string rdate;
                        cout << "Enter new Rent Date: " << endl;
                        cin >> rdate;
                        rdate = rent_date;
                    }

                    case 6:
                    {
                        string retdate;
                        cout << "Enter new Return Date: " << endl;
                        cin >> retdate;
                        retdate = return_date;
                    }

                    case 7:
                    {
                        string cnumber;
                        cout << "Enter new Copy Number: " << endl;
                        cin >> cnumber;
                        cnumber = copy_num;
                    }

                    case 8:
                    {
                        string rfee;
                        cout << "Enter new Rent Fee: " << endl;
                        cin >> rfee;
                        rfee = rent_fee;
                    }
                    case 9:
                    { break; }

                    }

                }
                else
                {
                    cout << "No data matched!" << endl;
                    break;
                }
            }

            myfile.close();
        }

    }

And this is the Txt File Created from Add Function

DF17361 | 12 | Drake | Comedy | Frank | 12/2/2014 | 23/2/2014 | 10

user32
  • 13
  • 1
  • 4
  • 1
    _'but it's not working.'_ What exactly is not working? Did you debug your code already going through step by step? – πάντα ῥεῖ Feb 23 '14 at 15:37
  • 1
    Don't loop using e.g. `while (!myfile.eof())`, it will not work as you expect. The reason being that the end-of-file flag is not set until *after* you tried to read from beyond the end of the file. So your loop will loop once to many. Instead do e.g. `while (myfile >> ...)` – Some programmer dude Feb 23 '14 at 15:37
  • 1
    I'm not really sure what you want to do. But you might have forgotten the `break` in your `switch`-statement (if you wanted to drop it is a good idea to add a comment that mentions the dropped `break`). – sleepy1771 Feb 23 '14 at 15:39
  • 2
    As for your problem, you can't really edit or modify a text file *in place*. Instead you can read the whole file into memory, change the thing you want changed, and write the whole file back out again. – Some programmer dude Feb 23 '14 at 15:39
  • And if you really want to edit file in place, then you need to use fixed size records, so you can overwrite existing records, or append new records at end (but still not insert new records in the middle). But that wouldn't really be classified as "text file" any more, as editing it with text editor would almost certainly break it. – hyde Feb 23 '14 at 16:09
  • @πάνταῥεῖ it has no errors, what i mean not working is the code implementation, I cannot change the string inside text file.. for example i want to change `Drake` to `Daniel`.. – user32 Feb 23 '14 at 16:09
  • Take on @JoachimPileborg's advcice! – πάντα ῥεῖ Feb 23 '14 at 16:10
  • @JoachimPileborg can you give me the code sample? – user32 Feb 23 '14 at 16:10
  • @user32 As a rough sketch: Provide a `DVD` class that allows to read from and write to a single line of text (from the file). For more about the topic read [here](http://stackoverflow.com/questions/234724/is-it-possible-to-serialize-and-deserialize-a-class-in-c). – πάντα ῥεῖ Feb 23 '14 at 16:18
  • @πάνταῥεῖ isn't that using ofstream and ifstream? i don't really get it :/ – user32 Feb 23 '14 at 16:30
  • Of course you can use `std::istream` `std::ostream` references to de-/serialize. You might want to overload the matching global `operator>>()` `operator<<()` for instances of `DVD` for instance. – πάντα ῥεῖ Feb 23 '14 at 16:34
  • I notice you read the new DVD ID in using: cin >> id; and then immediately overwrite the input id with: id = dvd_id, leaving the dvd_id as it originally was. If you were to write "dvd_id" to file at this point you would write the original value, not the new one. – Chris Feb 23 '14 at 19:22

0 Answers0