class Student
1: //binary_file6.cpp
2: //Illustrates input from and output to a binary file of class objects.
3: //Also illustrates "random" or "direct" access to those class objects
4: //from the open file.
6: #include <iostream>
7: #include <iomanip>
8: #include <fstream>
9: #include <string>
10: #include <vector>
11: using namespace std;
13: #pragma warning(disable:4996)
14: //strcpy deprecated? Pity ... only at Microsoft, you say.
16: class Student
17: {
18: public:
19: Student
20: (
21: string name="",
22: int age=0,
23: double balance=0.0
24: )
25: {
26: strcpy(this->name, name.c_str());
27: this->age = age;
28: this->balance = balance;
29: };
31: string getName() const
32: {
33: string name = this->name;
34: return name;
35: }
37: int getAge() const
38: {
39: return age;
40: }
42: double getBalance() const
43: {
44: return balance;
45: }
47: private:
48: char name[31];
49: int age;
50: double balance;
51: };
54: ostream& operator<<
55: (
56: ostream& out,
57: const Student& s
58: )
59: {
60: out << "Name: " << left << setw(11) << s.getName()
61: << "Age: " << left << setw(2) << s.getAge()
62: << " Balance: " << right << fixed
63: << setw(7) << setprecision(2) << s.getBalance() << endl;
64: return out;
65: }
68: int main()
69: {
70: const int NUMBER_OF_BYTES_IN_STUDENT = sizeof(Student);
71: fstream binaryIOFile("binaryfile5_inout.bin",
72: ios::trunc | ios::out | ios::in | ios::binary);
73: vector<Student> v(4);
74: v[0] = Student("John", 23, 123.50);
75: v[1] = Student("Mary", 19, 45.56);
76: v[2] = Student("Bob", 18, 234.87);
77: v[3] = Student("Al", 24, 23.76);
78: for (int i=0; i<4; i++)
79: binaryIOFile.write((char *)&v[i], NUMBER_OF_BYTES_IN_STUDENT);
80: cout << "\nInformation for four students has been written to a file."
81: "\nWe will now read that information from the file and display it.";
82: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');
84: cout << endl;
85: Student s;
86: binaryIOFile.seekg(0);
87: do
88: {
89: binaryIOFile.read((char *)&s, NUMBER_OF_BYTES_IN_STUDENT);
90: if (binaryIOFile) cout << s;
91: }
92: while (binaryIOFile);
93: binaryIOFile.clear();
94: cout << "Press Enter to continue ... "; cin.ignore(80, '\n');
96: cout << "\nNext, we will read from the file and display the"
97: "\ninformation for the third and second student students.";
98: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');
100: cout << endl;
101: binaryIOFile.seekg(2*NUMBER_OF_BYTES_IN_STUDENT, ios::beg);
102: binaryIOFile.read((char *)&s, NUMBER_OF_BYTES_IN_STUDENT);
103: cout << s;
104: binaryIOFile.seekg(-2*NUMBER_OF_BYTES_IN_STUDENT, ios::cur);
105: binaryIOFile.read((char *)&s, NUMBER_OF_BYTES_IN_STUDENT);
106: cout << s;
107: cout << "Press Enter to continue ... "; cin.ignore(80, '\n');
109: cout << "\nNow we add a new student to the end of the file,"
110: "\nthen read and display the entire file again.";
111: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');
113: cout << endl;
114: Student newStudent("Elizabeth", 17, 55.44);
115: binaryIOFile.seekg(0, ios::end);
116: binaryIOFile.write((char *)&newStudent, NUMBER_OF_BYTES_IN_STUDENT);
117: binaryIOFile.seekg(0);
118: do
119: {
120: binaryIOFile.read((char *)&s, NUMBER_OF_BYTES_IN_STUDENT);
121: if (!binaryIOFile.eof()) cout << s;
122: }
123: while (!binaryIOFile.eof());
124: binaryIOFile.clear();
125: cout << "Press Enter to continue ... "; cin.ignore(80, '\n');
127: cout << "\nNow we read from the file and display the"
128: "\ninformation for the fourth and fifth students.";
129: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');
131: cout << endl;
132: binaryIOFile.seekg(3*NUMBER_OF_BYTES_IN_STUDENT); //ios::beg is the default
133: binaryIOFile.read((char *)&s, NUMBER_OF_BYTES_IN_STUDENT);
134: cout << s;
135: binaryIOFile.read((char *)&s, NUMBER_OF_BYTES_IN_STUDENT);
136: cout << s;
137: cout << "Press Enter to continue ... "; cin.ignore(80, '\n');
139: cout << "\nFinally, we overwrite the second student in the file with "
140: "another\nnew student, and display the entire file contents one "
141: "more time.";
142: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');
144: cout << endl;
145: Student anotherNewStudent("Harry", 29, 321.54);
146: binaryIOFile.seekg(-4*NUMBER_OF_BYTES_IN_STUDENT, ios::end);
147: binaryIOFile.write((char *)&anotherNewStudent,
148: NUMBER_OF_BYTES_IN_STUDENT);
149: binaryIOFile.seekg(0);
150: do
151: {
152: binaryIOFile.read((char *)&s, NUMBER_OF_BYTES_IN_STUDENT);
153: if (!binaryIOFile.eof()) cout << s;
154: }
155: while (!binaryIOFile.eof());
156: binaryIOFile.clear();
157: cout << "\nThat's all, folks!\nProgram will now terminate.";
158: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');
160: binaryIOFile.close();
161: }