class Student
1: //binary_file4.cpp
2: //Illustrates input of class objects from both a textfile and a binary file.
4: #include <iostream>
5: #include <iomanip>
6: #include <fstream>
7: #include <string>
8: #include <list>
9: using namespace std;
11: #pragma warning(disable:4996)
12: //strcpy deprecated? Pity ... only at Microsoft, you say.
14: class Student
15: {
16: public:
17: Student
18: (
19: string name="",
20: int age=0,
21: double balance=0.0
22: )
23: {
24: strcpy(this->name, name.c_str());
25: this->age = age;
26: this->balance = balance;
27: };
29: string getName() const
30: {
31: string name = this->name;
32: return name;
33: }
35: int getAge() const
36: {
37: return age;
38: }
40: double getBalance() const
41: {
42: return balance;
43: }
45: private:
46: char name[31];
47: int age;
48: double balance;
49: };
52: //Note revision of this overloaded operator
53: //from previous program in this series.
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: ifstream textInFile("binary_file3.txt");
71: fstream binaryInFile("binary_file3.bin", ios::in | ios::binary);
73: list<Student> students;
74: string name;
75: textInFile >> name;
76: while (textInFile)
77: {
78: int age;
79: textInFile >> age;
80: double balance;
81: textInFile >> balance;
82: students.push_back(Student(name, age, balance));
83: textInFile >> name;
84: }
85: textInFile.close();
87: cout << "\nHere is a list of the students from the textfile:\n";
88: list<Student>::iterator pStudent = students.begin();
89: while (pStudent != students.end())
90: {
91: cout << pStudent->getName() << endl;
92: ++pStudent;
93: }
95: cout << "\nEnter any name to see that student's info.\n"
96: "Terminate by just pressing Enter.\n";
97: string who;
98: do
99: {
100: getline(cin, who);
101: if (who != "")
102: {
103: pStudent = students.begin();
104: while (pStudent != students.end() &&
105: pStudent->getName() != who) ++pStudent;
106: if (pStudent != students.end())
107: cout << *pStudent << endl;
108: else
109: cout << who << " is not a valid name.\n\n";
110: }
111: }
112: while (who != "");
113: cout << "Students from the binary file will now be added to the list.";
114: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');
116: Student s;
117: do
118: {
119: binaryInFile.read((char *)&s, sizeof(Student));
120: if (!binaryInFile.eof()) students.push_back(s);
121: }
122: while (!binaryInFile.eof());
123: binaryInFile.close();
125: cout << "\nHere is a list of all students from both files "
126: "(John appears twice):\n";
127: pStudent = students.begin();
128: while (pStudent != students.end())
129: {
130: cout << pStudent->getName() << endl;
131: ++pStudent;
132: }
134: cout << "\nEnter any name to see that student's info.\n"
135: "Terminate by just pressing Enter.\n";
136: do
137: {
138: getline(cin, who);
139: if (who != "")
140: {
141: pStudent = students.begin();
142: while (pStudent != students.end() &&
143: pStudent->getName() != who) ++pStudent;
144: if (pStudent != students.end())
145: cout << *pStudent << endl;
146: else
147: cout << who << " is not a valid name.\n\n";
148: }
149: }
150: while (who != "");
151: cout << "Program will now terminate.";
152: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');
153: }