First time implementing direct file access. What's the best way to do this?
This is the code I currently have. As you can see, its got a long way to go. What I need to do is be able to save and load the private data in the Book and Patron objects in such a way that I can access any given record without reading the whole thing. Strings have to be converted to old style C strings so that the Book and Patron records are all of fixed length so they can be saved in direct access files.
My current approach is bad (copying to char arrays w/o null characters (haven't figured that one out yet), filling unused cells with special characters, use sentinel characters to delimit the names and numbers, then, when loading, read different parts of the stream into different variables and so on. I don't like it at all. I know that there is a (much) better way. I missed that class though. So, I could use some guidance. What's the best way to do this?
Thanks for your time.
Code:
void Library::saveBooks(string save)
{
ofstream osavefile ( "Books.sav" );
for(int i = 1; i<bidnumber; i++)
{
char title[80];
char author[80];
char subject[80];
unsigned long loanedto = 1000000000;
// Added to borrower ID to make it a fixed length
// number in the file. substracted after reading.
if (LOB[i].getTitle() != "rumuration")
// rumuration is returned if a book ID is unused.(book deleted previously)
{
// strcpy is no good. Whole point of converting is making each
// data structure take the same size on file for direct access.
strcpy(author, (LOB[i].getAuthor()).c_str());
strcpy(subject, (LOB[i].getSubject()).c_str());
strcpy(title, (LOB[i].getTitle()).c_str());
loanedto = 1000000000 + LOB[i].getBorrower();
}
osavefile<<title<<author<<subject<<loanedto;
// even if BookID is unused, some padding should
// be written to file for random access to work, right?
}
osavefile.close();
}
Code:
void Library::load()
{
// Sacrifice goat and pray for miracle.
}
This is what Book and Patron objects look like (Patron inherits from Person which is also included).
Code:
#ifndef BOOK_H
#define BOOK_H
#include <string>
#include "Patron.h"
class Book
{
public:
Book();
//Removed bunch of accessor and mutator functions
private:
string title;
string author;
string subject;
unsigned long idNumber; //unique number for this book
unsigned long checkedOutByPatronID ; //will be NULL if the book is available
};
Code:
#ifndef PATRON_H
#define PATRON_H
#include "Person.h"
#include <iostream>
#define MAX_BOOKS 20
class ListOfBooks;
class Book;
class Patron : public Person
{
public:
Patron();
//Bunch of functions
private:
unsigned long idNumber; //unique number for this patron
unsigned long hasCheckedOutBookID[MAX_BOOKS];
unsigned short numberOfBooksCheckedOut;
};
#endif
Code:
#ifndef PERSON_H
#define PERSON_H
#include <string>
class Person
{
public:
Person();
void editName(string s);
string getName();
private:
string name;
int email;
};
#endif