#include #include #include using namespace std; template class IO { private: fstream file; int eof() { return file.eof(); } public: IO(const string& filename = "temp.bin") { file.open(filename,ios_base::in | ios_base::out | ios_base::trunc | ios_base::binary); } void rewind() { file.seekg(0L); file.seekp(0L); file.clear(); } IO& operator>>(T& t); IO& operator<<(const T& t); operator bool() { if (!file) return false; else return true; } }; template IO& IO::operator<<(const T& t) { file.write((char*) &t,sizeof(T)); return *this; } template IO& IO::operator>>(T& t) { file.read((char*)&t,sizeof(T)); return *this; } class A { int a; public: friend istream& operator>>(istream& in, A& AA); friend ostream& operator<<(ostream& out, A& AA); }; istream& operator>>(istream& in, A& AA) { cout << "Enter an int for an A object => "; return in >> AA.a; } ostream& operator<<(ostream& out, A& AA) { return out << AA.a; } class B { protected: double bl; char b2[16] ; long b3; public: friend istream& operator>>(istream& in, B& BB); friend ostream& operator<<(ostream& out, B& BB); }; istream& operator>>(istream& in, B& BB) { cout << "Enter double, char* and long for a B object => "; return in >> BB.bl >> BB.b2 >> BB.b3; } ostream& operator<<(ostream& out, B& BB) { return out << BB.bl << ' ' << BB.b2 << ' ' << BB.b3; } int main(void) { A apple; IO appleIO("apple.bin"); cin >> apple; appleIO << apple; cin >> apple; appleIO << apple; B banana; IO bananaIO("banana.bin"); cin >> banana; bananaIO << banana; cin >> banana; bananaIO << banana; cin >> banana; bananaIO << banana; int temp; IO intIO; intIO << rand() % 100; intIO << rand() % 100; intIO << rand() % 100; intIO << rand() % 100; intIO << rand() % 100; appleIO.rewind(); while (appleIO >> apple) cout << apple << endl; bananaIO.rewind(); while (bananaIO >> banana) cout << banana << endl; intIO.rewind(); while (intIO >> temp) cout << temp << endl; }