Append to binary file?

i am probably not including the ios::app correctly..... it won't let me append to the critters.bin file

Here is my code:

//header


struct Cat
{
char name[20];
int age;


};


///////////////////



#include <iostream>
#include <string>
#include "Cat.h"
#include <fstream>
using namespace std;



int main()
{
Cat yourCat;
int num = 3;

fstream file;
cout << "Enter 3 cat records.\n";

file.open("c:\\critters.bin", ios::out | ios::binary);

for(int i = 0; i < 3; i++)
{
cout << "Enter information about a cat:\n";
cout << "NAME: ";
cin >> yourCat.name;
cout << "AGE: ";
cin >> yourCat.age;
file.write(reinterpret_cast<char*>(&yourCat), sizeof(Cat));
}


while(file >> num)
{
file.write(reinterpret_cast<char*>(&yourCat), sizeof(Cat));
}
cout << "Record written to file.\n";


cout << "\n\nEnter one more cat\n";
file.open("critters.bin", ios::app);
cout << "NAME: ";
cin >> yourCat.name;
cout << "AGE: ";
cin >> yourCat.age;
file.write(reinterpret_cast<char*>(&yourCat), sizeof(Cat));


cout << "\n\nHere is a list of all cats:\n";



system("pause");
return 0;
}
Topic archived. No new replies allowed.