Source of binary_file3.cpp


  1: //binary_file3.cpp
  2: //Illustrates output of class objects to both a textfile and a binary file.

  4: #include <iostream>
  5: #include <fstream>
  6: #include <string>
  7: #include <vector>
  8: using namespace std;

 10: #pragma warning(disable:4996)
 11: //strcpy deprecated? Pity ... only at Microsoft, you say.

 13: class Student
 14: {
 15: public:
 16:     Student
 17:     (
 18:         string name="",
 19:         int age=0,
 20:         double balance=0.0
 21:     )
 22:     {
 23:         strcpy(this->name, name.c_str());
 24:         this->age = age;
 25:         this->balance = balance;
 26:     };

 28:     string getName() const
 29:     {
 30:         string name = this->name;
 31:         return name;
 32:     }

 34:     int getAge() const
 35:     {
 36:         return age;
 37:     }

 39:     double getBalance() const
 40:     {
 41:         return balance;
 42:     }

 44: private:
 45:     char name[31];
 46:     int age;
 47:     double balance;
 48: };


 51: ostream& operator<<
 52: (
 53:     ostream& out,
 54:     const Student& s
 55: )
 56: {
 57:     out << s.getName() << " "
 58:         << s.getAge()  << " "
 59:         << s.getBalance() << endl;
 60:     return out;
 61: }


 64: int main()
 65: {
 66:     cout << "\nThis program writes several Student class objects "
 67:         "\nfirst to a text file, and then to a binary file.";
 68:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 70:     Student s("John", 23, 123.50);

 72:     cout << "\nFirst we write out the information for a single Student "
 73:         "\nfrom a simple Student variable to both files.";
 74:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');
 75:     ofstream textOutFile("binary_file3.txt");
 76:     textOutFile << s;

 78:     fstream binaryOutFile("binary_file3.bin",
 79:         ios::trunc | ios::out | ios::binary);
 80:     binaryOutFile.write((char *)&s, sizeof(Student));

 82:     cout << "\nFinally, we create and populate a vector of seven Student "
 83:         "objects. Then we\nwrite the first three to the textfile and the "
 84:         "last four to the binary file.";
 85:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');
 86:     vector<Student> v(7);
 87:     v[0] = Student("Elizabeth", 19, 45.56);
 88:     v[1] = Student("Bob", 18, 234.87);
 89:     v[2] = Student("Al", 24, 23.76);
 90:     v[3] = Student("Samuel", 58, -245.92);
 91:     v[4] = Student("William", 33, 199.99);
 92:     v[5] = Student("Mohammed", 29, 61.67);
 93:     v[6] = Student("Qiyan", 31, 79.12);
 94:     for (int i=0; i<3; i++)
 95:         textOutFile << v[i];
 96:     for (int i=3; i<7; i++)
 97:         binaryOutFile.write((char *)&v[i], sizeof(v[i]));
 98:     cout << "\nOutput to binary_file3.txt and binary_file3.bin completed.";
 99:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

101:     textOutFile.close();
102:     binaryOutFile.close();
103: }