Read file and write to same file

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
//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.
1
2
3
4
5
add_book(books, num_records);
//exploting the function
   getline(cin, books[num_records].author);
   num_records++;
cout<<books[num_records].author;
¿do you see your error?


Also, your reading will invalidate the stream. You need to .clear() it before operating on it.
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.
Ok, I follow you now and see that mistake. I did a test with cout and got it to work with this,

1
2
3
4
case 3:
     add_book(books, num_records);
     cout<< endl << books[num_records - 1].author << endl;
     break;


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.
Topic archived. No new replies allowed.