hello everyone. I was making a project for college library management system with C++(using file handling and OOPS). I am told to keep a live record of books in a file(.txt or .dat). Now if a book is issued, its count will be lessened by 1. HOW TO UPDATE THE COUNT IN THE TXT OR DAT FILE. E.g. if a book(OBJECT) is issued and it had 22 copies. now it has 21 copies. how to edit update that 22 to 21 in the file. YOU CAN VIEW THE PROJECT I MADE TILL NOW here..... https://github.com/js899/LIBRARY
for small things it is easier to just rewrite the entire file with the updated info.
you could find the exact spot and over-write the file after updating just the affected data but its a bit harder (its easier for binary files to find the right spot to edit. you could make your text file a binary file with fixed width string locations using a sensible over-sized width... pad the extra space with spaces ...
It is an jonnin said. It's probably much easier for you to just read everything in the file into some class like the below (assuming you haven't learned about binary files).
1 2 3 4 5 6 7 8 9
class Books {
public:
// Some getter and setter functions in public
private:
std::string authorFirstName;
std::string authorLastName;
std::string bookTitle;
unsignedint copies;
};
Then once you need to access the library, you would just use a getter or setting function to do so. To write or update the library, you would overwrite using the an object from std::ofstream like so:
1 2 3 4
void updateFunction(Books& obj) {
// update function to open the file and rewrite everything, thus updating the
// values
}