C++ ifstream Reads person object from a file
#include <fstream> #include <iostream> using namespace std; class person/*from w ww. j ava2 s.c o m*/ { protected: char name[80]; //person's name short age; //person's age public: void showData() //display person's data { cout << "Name: " << name << endl; cout << "Age: " << age << endl; } }; int main() { person pers; ifstream infile("PERSON.DAT", ios::binary); //create stream //read stream infile.read( reinterpret_cast<char*>(&pers), sizeof(pers) ); pers.showData(); return 0; }