okay, I've been trying this all day and I can't get it to work for the life of me. I'm trying to fill an object from a data file then possibly make changes to it and put it back.
the data file looks like this:
Battlefield Earth //title of a book
L. Ron Hubbard //author of the book
3 //copies of the book in the database
15.95 //price of the book
here's the class headers:
Code:
class Book
{
public:
void operator = (const Book& b1);
Book(); //sets title and author to empty strings
//copies to 0 and price to 0.0
string get_title(); //returns title
string get_author(); //returns author
void set_title(string str); //changes value of title
void set_author(string str); //changes value of author
void set_copies(int num); //changes value of copies
void set_price(double p); //changes value of price
int get_copies(); //returns copies
double get_price(); //returns price
void print(ostream& outs); //outputs all the data in the format
//I showed above
void fill(istream& ins); //fills the values
private:
string title;
string author;
int copies;
double price;
};
//here's the second class
typedef Book* Bookptr;
class Library
{
public:
~Library();
Library(const Library& l1);
void operator = (const Library& l1);
Library(); //sets size to 1 and allocates memory for books
void search (); //searches by author or title
void print(ostream& outs); //prints books in format above
void add_new(const string& s1, fstream& file); //adds a new
//book to the library or increments the number of copies if it is
//already in the database. this is the function I've been having
//trouble with
void fill(istream& ins, int& num); //input function
void total(); //adds up price of all the books and prints it
void delete_book(const string& s1); //deletes a book from the
//library, another one I'm having trouble with
void check_out(const string& s1); //checks a book out
void random(); //prints a random book
private:
int size;
Bookptr books;
void resize(Bookptr& b1, int& num);
};
//all of these functions work really well, the only problem I'm
//having is that my data file never gets permanently edited even
//when I want to. here's the definitions of the functions that
//matter
void add_new(const string& s1, fstream& file)
{
Book b1;
string str;
int temp;
double temp_price;
for (int i = 0; i < size; i++)
{
if (new_title == books[i].get_title())
{
temp = books[i].get_copies() + 1;
books[i].set_copies(temp);
}
return;
}
//if the book isn't in the database already, the rest of this
//function gets its info from the keyboard, puts it into the books
//array, and prints it into the data file. that works fine, the part
//above is what I need help with. I need to know how to scrap
//the old value of copies and print the new one out to the same
//place. I was thinking of something like this:
file.open("database.dat",ios::out|ios::trunc);
print(file);
file.close();
//but every time I try that it just deletes the data file and
//doesn't fill it again
ultimately what I'm going to need to be able to is open the file and fill a Library object from it. then I need to be able to edit it. and I need to be able to change individual lines of it. I can't figure out how for the life of me. the easiest way I think would be to delete the file then reprint the new data
if you need more code I can provide it, but I can't think of any that you'd really need. none of the other functions are called in this piece of code