Добавить k элементов структуры в конец файла
Программа должна добавить элементы структуры, вывести содержимое файла, а затем добавить элементы, количество которых вводится с клавиатуры (сами элементы структуры тоже вводятся вручную). Вот код, который у меня получился, он, почему-то, не добавляет элементы в конец.
#include <iostream>
#include <fstream>
using namespace std;
struct book
{
std::string name;
std::string author;
int year;
int CountOfPages;
};
int main() {
book *mas;
fstream f("f.dat", ios::out);
int n;
cout<<"N?";
cin>>n;
mas=new book[n];
for (int i = 0; i < n; i++)
{
cout<<"?";
cin>>mas[i].name;
cin>>mas[i].author;
cin>>mas[i].year;
cin>>mas[i].CountOfPages;
}
for (int i = 0; i < n; i++)
{
f<<mas[i].name;f<<"\n";
f<<mas[i].author;f<<"\n";
f<<mas[i].year;f<<"\n";
f<<mas[i].CountOfPages;f<<"\n";
}
f.close();
book b;
f.open("f.dat", ios::in);
do
{
f>>b.name;
f>>b.author;
f>>b.year;
f>>b.CountOfPages;
if(f.eof())break;
cout<<b.name<<" "<<b.author<<" "<<b.year<<" "<<b.CountOfPages<<"\n";
} while (!f.eof());
f.close();
int k;
cout<<"K?";
cin>>k;
book *temp;
temp = new book[k];
for (int i = 0; i < k; i++)
{
cout<<"?";
cin>>temp[i].name;
cin>>temp[i].author;
cin>>temp[i].year;
cin>>temp[i].CountOfPages;
}
f.open("f.dat", ios::ate);
for (int i = 0; i < k; i++)
{
f<<temp[i].name;f<<"\n";
f<<temp[i].author;f<<"\n";
f<<temp[i].year;f<<"\n";
f<<temp[i].CountOfPages;f<<"\n";
}
f.close();
f.open("f.dat", ios::in);
do
{
f>>b.name;
f>>b.author;
f>>b.year;
f>>b.CountOfPages;
if(f.eof())break;
cout<<b.name<<" "<<b.author<<" "<<b.year<<" "<<b.CountOfPages<<"\n";
} while (!f.eof());
f.close();
return 0;
}