2017-08-13 375 views
-1

我为库系统制作了一个循环来显示此菜单。如何使用C++删除/更新txt文件中的特定数据

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它告诉我,它不存在,但在程序开始时,它告诉我,该书存在(这是正确的) 我该如何解决这个问题,哪里的错误。

摘要:当我搜索了2倍以上的现有标识,第一时间它工作正常,其余的给我看,这本书不存在

显示所有书籍代码: -

在UpdateOrDel功能
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; 
    } 

借代码: -

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

回答

2

ILIB(我认为这是你的文件,因为你没有显示它的定义),包含一个指向FIL内的位置即如果你想从一开始一个新的搜索,你需要将其设置为再次第一个开始:

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

非常感谢,它的工作的大多数菜单的,但是当我借一本书,然后尝试显示它没有显示任何内容的所有书籍。 更新了更多细节 – MiDo

相关问题