如何使用C ++删除/更新txt文件中的特定数据

时间:2017-08-13 15:27:20

标签: c++ file

我为图书馆系统制作了一个循环来显示这个菜单。

cout << "1. Add a new book.\n2. Update a book.\n3. Delete a book.\n4. Show all 
books.\n5. Search book by ID.\n6. Search book by Name.\n7. Borrow a book.\n8. 
Exit." << endl;
cin >> choice;
while (choice >= 0 && choice <8) {
if (choice == 5) {
        int x;
        cout << "Enter the ID you want to search for" << endl;
        cin >> x;
        SearchByID(x);
        choice = 0;
    }
 cout << "1. Add a new book.\n2. Update a book.\n3. Delete a book.\n4. Show 
 all books.\n5. Search book by ID.\n6. Search book by Name.\n7. Borrow a 
 book.\n8. exit." << endl;
 cin >> choice;
 }

SearchByID函数就是这样编码的。

void SearchByID(int y)
{
bool found = false;
Book book;
string id, name, author, price, copies;
iLib >> id >> name >> author >> price >> copies;
while (iLib >> book.ID >> book.name >> book.author >> book.price >> book.copies) {
    if (book.ID == y) {
        cout << "Book is found:" << endl;
        cout << left << setw(4) << id << setw(15) << name << setw(15) << author << setw(8) << price << setw(8) << copies << endl;
        cout << left << setw(4) << book.ID << setw(15) << book.name << setw(15) << book.author << setw(8) << book.price << setw(8) << book.copies << endl << endl << endl;
        found = true;
        break;
    }
}
if (found == false)
    cout << "Book is not found" << endl;
}

当我运行它并通过id搜索一本书时它工作正常,但是当功能工作完成时,while循环再次显示此菜单,如果我搜索相同的书ID,它会告诉我它不存在但是在节目开始时它告诉我这本书存在(这是正确的) 我怎么能解决这个问题以及错误的位置。

摘要:当我搜索现有ID 2次或更多时,第一次工作正常,其余的则告诉我该书不存在

显示所有图书代码: -

if (choice == 4) { // show all books
        string id, name, author, price, copies;
        iLib >> id >> name >> author >> price >> copies;
        cout << left << setw(4) << id << setw(15) << name << setw(15) << author << setw(8) << price << setw(8) << copies << endl;
        while (iLib >> book.ID >> book.name >> book.author >> book.price >> book.copies) {
            cout << left << setw(4) << book.ID << setw(15) << book.name << setw(15) << book.author << setw(8) << book.price << setw(8) << book.copies << endl;
            cout << "================================================" << endl;
        }
        iLib.clear();
        iLib.seekg(0);
        choice = 0;
    }

在UpdateOrDel函数中借用代码: -

void UpdateOrDel(int id, int s) {
if (s == 3) { // borrow
    while (!iLib.eof() && i <= 1) {
        string idd, name, author, price, copies;
        iLib >> idd >> name >> author >> price >> copies;
        oTmpLib << left << setw(4) << idd << setw(15) << name << setw(15) << author << setw(8) << price << setw(8) << copies << endl;
        i++;
    }
    while (iLib >> b.ID >> b.name >> b.author >> b.price >> b.copies) {
        if (b.ID == id) {
            found = true;
            oTmpLib << left << setw(4) << b.ID << setw(15) << b.name << setw(15) << b.author << setw(8) << b.price << setw(8) << b.copies - 1 << endl;
            cout << "Book has been Borrowed! \n \n" << endl;
        }
        else
            oTmpLib << left << setw(4) << b.ID << setw(15) << b.name << setw(15) << b.author << setw(8) << b.price << setw(8) << b.copies << endl;
    }
    iLib.clear();
    iLib.seekg(0);
    OLib.close();
    iLib.close();
    remove("library.txt");
    ofstream OLib("library.txt", ios::app);
    ifstream iLib("library.txt", ios::in);
    i = 1;
    while (!iTmpLib.eof() && i <= 1) {
        string idd, name, author, price, copies;
        iTmpLib >> idd >> name >> author >> price >> copies;
        OLib << left << setw(4) << idd << setw(15) << name << setw(15) << author << setw(8) << price << setw(8) << copies << endl;
        i++;
    }
    while (iTmpLib >> b.ID >> b.name >> b.author >> b.price >> b.copies) {
        OLib << left << setw(4) << b.ID << setw(15) << b.name << setw(15) << b.author << setw(8) << b.price << setw(8) << b.copies << endl;
    }
    iTmpLib.close();
    oTmpLib.close();
    remove("LibraryTemp.txt");
    if (found == false)
        cout << "Book is not found \n \n" << endl;
   }
}

1 个答案:

答案 0 :(得分:2)

iLib(我认为是你的文件,因为你没有显示它的定义)包含一个指向文件内部位置的指针。如果您想从头开始进行新搜索,则需要先将其设置为开头:

iLib.clear();  
iLib.seekg(0);