在C ++中编辑文本文件中的特定字符串

时间:2014-02-23 15:35:24

标签: c++

如何编辑文本文件中的特定字符串?

我使用我的逻辑,但它不起作用..

非常感谢任何人帮助我

这是我的添加功能

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");

}

这是编辑功能

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

    }

这是从添加功能创建的 Txt文件

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

0 个答案:

没有答案