搜索结构C ++的.dat文件

时间:2015-11-12 07:12:37

标签: c++

我正在处理图书库存计划,但我的searchBook()功能遇到问题。我希望能够在bookfile中搜索子字符串的每个实例,并显示每个书籍的标题和作者,用户可以从中选择他们想要查看完整结构的书籍。 我的结构:

struct Book {
    string title, author, pubDate, synop, isbn;
}b[1000];

Book.dat最多包含1000个这些结构(忽略颜色):

Book Title
Last, First
11/12/2015
This book is about...
1234567890

我的搜索功能:

void searchBooks(ifstream& bookfile, Book b){
    int quit = 0;
    do{
        if (!bookfile){
            cout << "ERROR: book.dat not found...nothing to do here.";
            exit(EXIT_FAILURE);
        }
        string s;
        while (getline(bookfile, s))
            bookfile.close();
        cout << "Enter search: ";
        string searchBook;
        getline(cin, searchBook);
        vector<size_t> positions;
        do
        {
            if(s.find(searchBook) != string::npos){
                size_t pos = s.find(searchBook, 0);
                positions.push_back(pos);
                pos = s.find(searchBook,pos+1);
                cout << "Found " << searchBook << " at line " << pos << endl;
            }
            else {
                while (string :: npos) {
                    cout << "End of file reached...Search again: ";
                    getline(cin, searchBook);
                }
            }
        } while(getline(bookfile, searchBook));
        cout << "What do you want to do with this line?" << endl;
        cout << "1-Edit" << endl;
        cout << "2-Delete" << endl;
        cout << "3-Search Again" << endl;
        cout << "4-Quit" << endl;
        int sl;
        cin >> sl;
        switch (sl) {
            case 1:
                editBook(bookfile, searchBook);
                break;

            case 2:
                deleteBook(bookfile, searchBook);
                break;
            case 3:
                searchBooks(bookfile, b);
            case 4:
                quit = 1;
                break;
        }
    } while (quit !=1);
}

0 个答案:

没有答案