// binary.cpp -- binary file I/O #include <iostream> #include <fstream> #include <iomanip> #include <cstdlib> //for exit() using namespace std; const char * file = "planets.dat"; struct planet { char name[20]; //name of planet double population; //its population double g; //its acceleration of gravity }; inline void eatline() { while(cin.get() != ' ') { continue; } } int main() { planet pl; cout << fixed << right; //show initial contents ifstream fin; fin.open(file, ios_base::in | ios_base::binary); //binary file if(fin.is_open()) { cout << "Here are the current contents of the " << file << " file:" << endl; while(fin.read((char* )&pl, sizeof pl)) { cout << setw(20) << pl.name << ": " << setprecision(0) << setw(12) << pl.population << setprecision(2) << setw(6) << pl.g << endl; } fin.close(); } //add new data ofstream fout; fout.open(file, ios_base::out | ios_base::app | ios_base::binary); if(!fout.is_open()) { cerr << "Can't open " << file << " file for output:" << endl; exit(EXIT_FAILURE); } cout << "Enter planet name (enter a blank line to quit):" << endl; cin.get(pl.name, 20); while(pl.name[0] != '