I know this topic has been covered in multiple discussions and I have read quite a few, but I still can't seem to grasp the concept. I will also let any potential responders know that this was a homework assignment that I have already turned in. The write to file portion is for extra credit, but even though I didn't finish, I still want to know how to write the proper code for my desired outcome. So, that being said, my program opens a file and gives the user options to display the file, display part of the file based on input, add to the file, save info to a new file or quit. Everything works properly except adding to file. I can add to a structure within the program and that display properly but it doesn't seem to write to the file. The program is quite long for me so I will try to post only the necessary code. If you want/need to see more just let me know.
//Global variable for the maximum number of books.
int NUM_RECORDS = 100;
//Structure definition for book information.
struct Book
{
string author;
string title;
string publisher;
string date;
char status;
};
... later in code
int main()
{
Book books[NUM_RECORDS];
fstream input_file("books.data", ios::in | ios::out | ios::app);
int num_records = 0;
int user_choice = 0;
char category = ' ';
string line;
//This statement lets user know if file doesn't open.
if (input_file.fail())
{
cout <<"Input file opening failed! Aborting...\n";
exit(1);
}
//This loop adds file info to books array.
while(!input_file.eof())
{
getline(input_file, line);
if(line.length() > 0)
{
books[num_records] = make_book(line);
num_records++;
}
}
....later in code
case 3:
add_book(books, num_records);
input_file<< books[num_records].author;
cout<<books[num_records].author;
break;
...sub function for code
void add_book(Book add[], int &records)
{
if(records < NUM_RECORDS)
{
getchar();
cout<< endl << "Adding a new book..." << endl;
cout<< " Enter author: ";
getline(cin, add[records].author);
add[records].author.resize(38, ' ');
cout<< " Enter title: ";
getline(cin, add[records].title);
add[records].title.resize(53, ' ');
cout<< " Enter publisher: ";
getline(cin, add[records].publisher);
add[records].publisher.resize(19, ' ');
cout<< " Enter date: ";
getline(cin, add[records].date);
add[records].date.resize(6, ' ');
cout<< " Enter category (1-letter code): ";
cin>> add[records].status;
records++;
}
else
{
cout<< "Database is full, no more books can be added." << endl;
}
}
I believe the problem has to do with using num_records when trying to write, as i also tried a cout to the screen to test which does not print anything either. What I want is for the contents of the added structure to be appended to the file. I just used the author part to test it, but when I figure it out I will add each part of the structure. Any help is greatly appreciated.
I am sorry, still fairly new to coding and I don't understand what you mean by exploiting the function. I will try to do some more research related to using .clear() and see what I can come up with.
sorry, it should be "exploding the function". I wrote the body of `add_book()' function adjusting the name of the parameters.
what wanted to show is that you read into the n element, but are showing the n+1 element.
I tried to make another int variable and set it to num_records -1 but it gave me a permissive error, so I went with this and it worked. So now you are telling me I need to learn how to use the .clear() in order to append it to the existing file? I have also seen many people discussing the seekp() function. I am unfamiliar with both of these functions, but I can look them up in my book and see if I can learn what each is for and how to use them. Thanks for showing me what I was doing wrong with the n element though.
I have added the .clear function and it has solved some more problems. I have a few issues left, but with a little more research I believe I can solve those too. Again, ne555, your assistance in pointing out the flaw in my code and pointing me in the right direction is greatly appreciated.