1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
|
#include <iostream>
#include <fstream>
#include "Student.h"
using namespace std;
void displayStudent(Student student)
{
cout << student.getFirstName() << " ";
cout << student.getMi() << " ";
cout << student.getLastName() << " ";
cout << student.getScore() << endl;
};
int main()
{
fstream binaryio; // Create stream object
binaryio.open("student.dat", ios::out | ios::binary);
Student student1("John", 'T', "Smith", 90);
Student student2("Eric", 'K', "Jones", 85);
binaryio.write(reinterpret_cast<char *>(&student1), sizeof(Student));
binaryio.write(reinterpret_cast<char *>(&student2), sizeof(Student));
binaryio.close();
// Read student back from the file
binaryio.open("student.dat", ios::in | ios::binary);
Student studentNew1 = Student("Jo", 'T', "Schmo", 99);
Student studentNew2 = Student("Jo", 'T', "Schmo", 99);
binaryio.read(reinterpret_cast<char *>(&studentNew1), sizeof(Student));
displayStudent(studentNew1);
binaryio.read(reinterpret_cast<char *>(&studentNew2), sizeof(Student));
displayStudent(studentNew2);
binaryio.close();
return 0;
};
|