The text format is easy to read. With it, you can use an ordinary editor or wrd processor to read and edit a text file.
The binary format is more accurate for numbers because it stores the exact internal representation of a value. There are no conversion errors or round-off errors.
// text format
ofstream fout("planets.dat", ios_base::out| ios_base::app);
fout<<p1.name <<" " << p1.population<<" "<<p1.g<<"\n";
// binary format
ofstream fout("planets.dat", ios_base::out| ios_base::app | ios_base::binary);
fout.write( (char *(&p1, sizeof p1);
ifstream fin("planets.dat", ios_base::in | ios_bas::binary);
fin.read((char *) &p1, sizeof p1):
The write() function copeis a specified number of bytes from memory o a file.