0

I am developing a library management system in C++ and have implemented a function to handle book returns. The function reads book records from a file, checks if a book is issued, and then writes the updated records to a temporary file. When I attempt to return a book that is not issued, the function correctly displays a message saying that the book was not issued. However, the book record is not written to the temporary file, which causes the record to be missing in the updated file. The problem is that when the book is not issued, the else block (for non-issued books) is executed, but the book record is not written to the temporary file.

I changed the else statement to a separate if statement as:

if (!issued)
{
    outfile << title << ", " << author << ", " << ISBN << ", " << issued << endl;
}

but it didn't solve the problem. The code for return function is :

void LIBRARIAN::Book_return() {
    cout << "Enter the title of book you want to return: ";
    cin.ignore();
    getline(cin, search);

    bool found = false;
    string line;
    ifstream infile("library_Data.txt", ios::in);
    ofstream outfile("temp_Data.txt", ios::out);

    if (!infile.is_open() || !outfile.is_open()) {
        cout << "\nUnable to open the file(s)\n";
        return;
    }
    while (getline(infile, line)) {
        string title, author, ISBN;
        bool issued;
        istringstream linestream(line);

        if (getline(linestream, title, ',')) {
            getline(linestream, author, ',');
            getline(linestream, ISBN, ',');
            linestream >> issued;
            if (title == search) {
                if (issued) {
                    issued = false;
                    outfile << title << ", " << author << ", " << ISBN << ", "
                            << issued << endl;
                    cout << "\nBook returned successfully!" << endl;
                } else {
                    cout << "\nBook was not issued!" << endl;
                }
                found = true;
            } else
                // Write unchanged lines to temp file
                outfile << title << ", " << author << ", " << ISBN << ", "
                        << issued << endl;

            infile.close();
            outfile.close();
            if (!found) {
                cout << "\nBook not available!" << endl;
            }
            // Error handling for file operations
            if (remove("library_Data.txt") != 0) {
                cout << "Error deleting the original file.\n";
                return;
            }
            if (rename("temp_Data.txt", "library_Data.txt") != 0) {
                cout << "Error renaming the temporary file.\n";
                return;
            }
        }

How can I ensure that the non-issued book record is correctly written to the temp file?

2
  • Read the file at the start into a std::vector of struct Book. Then do changes to vector elements (example, update issued). Then write the vector of books to the file (overwrite) at the end. No need of maintaining temporary files. Commented Aug 1, 2024 at 10:47
  • The calls to infile.close() and outfile.close() inside the while loop smells dodgy, to my nasal debugger. Commented Aug 1, 2024 at 19:08

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.