Library project

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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
FUNCTION TO ISSUE BOOK:

cout<<"ENTER ONE MORE INFO";
            string sassy;
            cin>>sassy;
            int found;
            while(fin){
                string s;
                getline(fin, s);
                found = s.find(sassy);
                if(found){
                break;
                }
            }
            if(found!=string::npos){
                cout<<"ISSUED"<<endl;
                    b.nofcopies--;
                    ofstream fout;
                    fout<<b.nofcopies--;
            }
        }


i know this is not correct, just posted it. Please help.
Last edited on
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 ...
Last edited on
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;
    unsigned int 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
}
Topic archived. No new replies allowed.