To indicate that you want to save a value as binary, when declaring the ofstream
variable, specify the ios option as binary. Here is an example:
#include <fstream>
#include <iostream>
using namespace std;
class Student
{
public:
char FullName[40];
char CompleteAddress[120];
char Gender;
double Age;
bool LivesInASingleParentHome;
};
int main()
{
Student one;
strcpy(one.FullName, "Ernestine Waller");
strcpy(one.CompleteAddress, "824 Larson Drv, Silver Spring, MD 20910");
one.Gender = 'F';
one.Age = 16.50;
one.LivesInASingleParentHome = true;
ofstream ofs("fifthgrade.ros", ios::binary);
return 0;
}
Practical Learning: Specifying Binary Format
|
|
- In the source file of the program, create a structure named
CStudentGrade and that has a member variable equivalent to each value
in the dialog box:
// ROSH1Dlg.cpp : implementation file
//
#include "stdafx.h"
#include "ROSH1.h"
#include "ROSH1Dlg.h"
#include ".\rosh1dlg.h"
#include <fstream>
using namespace std;
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// CROSH1Dlg dialog
struct CStudentGrade
{
CString StudentName;
int SchoolYear1;
int SchoolYear2;
double English;
double History;
double Economics;
double Language2;
double Geography;
double Arts;
double Math;
double Science;
double PhysEduc;
double Total;
double Average;
};
|
- Return to the dialog box and double-click the Save button
- Implement its event as follows:
void CROSH1Dlg::OnBnClickedSave()
{
// TODO: Add your control notification handler code here
CStudentGrade StdGrades;
CString strFilename = this->m_StudentName + ".dnt";
ofstream ofs(strFilename, ios::binary);
}
|
- Save all
The ios::binary option lets the compiler know
how the value will be stored. This declaration also initiates the file. To
write the values to a stream, you can call the fstream::write()
method.
After calling the write() method, you can write the
value of the variable to the medium. Here is an example:
#include <fstream>
#include <iostream>
using namespace std;
class Student
{
public:
char FullName[40];
char CompleteAddress[120];
char Gender;
double Age;
bool LivesInASingleParentHome;
};
int main()
{
Student one;
strcpy(one.FullName, "Ernestine Waller");
strcpy(one.CompleteAddress, "824 Larson Drv, Silver Spring, MD 20910");
one.Gender = 'F';
one.Age = 16.50;
one.LivesInASingleParentHome = true;
ofstream ofs("fifthgrade.ros", ios::binary);
ofs.write((char *)&one, sizeof(one));
return 0;
}
Practical Learning: Writing to the Stream
|
|
- Change the code of the Save button as follows:
void CROSH1Dlg::OnBnClickedSave()
{
// TODO: Add your control notification handler code here
UpdateData();
CStudentGrade StdGrades;
// Save a student's grades only if the record contains a name
if( this->m_StudentName == "" )
return;
// Don't save the record if the school year is not known or is not valid
if( this->m_SchoolYear1 < 1900 )
return;
if( this->m_SchoolYear2 != (this->m_SchoolYear1 + 1) )
return;
// Initialize the student with the values from the dialog box
strcpy(StdGrades.StudentName, this->m_StudentName);
StdGrades.SchoolYear1 = this->m_SchoolYear1;
StdGrades.SchoolYear2 = this->m_SchoolYear2;
StdGrades.English = this->m_English;
StdGrades.History = this->m_History;
StdGrades.Economics = this->m_Economics;
StdGrades.Language2 = this->m_2ndLanguage;
StdGrades.Geography = this->m_Geography;
StdGrades.Arts = this->m_Arts;
StdGrades.Math = this->m_Math;
StdGrades.Science = this->m_Science;
StdGrades.PhysEduc = this->m_PhysEduc;
StdGrades.Total = this->m_Total;
StdGrades.Average = this->m_Average;
CString strFilename = this->m_StudentName;
CFile fleGrades;
char strFilter[] = { "Student Grades (*.dnt)|*dnt|All Files (*.*)|*.*||" };
CFileDialog dlgFile(FALSE, ".dnt", strFilename, 0, strFilter);
if( dlgFile.DoModal() == IDOK )
{
// Save the record in binary format
ofstream stmGrades(dlgFile.GetFileName(), ios::binary);
stmGrades.write((char *)&StdGrades, sizeof(StdGrades));
// Reset the dialog box in case the user wants to enter another record
m_StudentName = "";
m_SchoolYear1 = 2000;
m_SchoolYear2 = 2001;
m_English = 0.00;
m_History = 0.00;
m_Economics = 0.00;
m_2ndLanguage = 0.00;
m_Geography = 0.00;
m_Arts = 0.00;
m_Math = 0.00;
m_Science = 0.00;
m_PhysEduc = 0.00;
m_Total = 0.00;
m_Average = 0.00;
}
UpdateData(FALSE);
}
|
- Execute the application
- Enter sample grades for a student and click Calculate
- Click the Save button. Accept the default name of the file and click
Save
- Close the dialog box and return to your programming environment
Reading an object saved in binary format is as easy as
writing it. To read the value, call the ifstream::read() method. Here is
an example:
#include <fstream>
#include <iostream>
using namespace std;
class Student
{
public:
char FullName[40];
char CompleteAddress[120];
char Gender;
double Age;
bool LivesInASingleParentHome;
};
int main()
{
/* Student one;
strcpy(one.FullName, "Ernestine Waller");
strcpy(one.CompleteAddress, "824 Larson Drv, Silver Spring, MD 20910");
one.Gender = 'F';
one.Age = 16.50;
one.LivesInASingleParentHome = true;
ofstream ofs("fifthgrade.ros", ios::binary);
ofs.write((char *)&one, sizeof(one));
*/
Student two;
ifstream ifs("fifthgrade.ros", ios::binary);
ifs.read((char *)&two, sizeof(two));
cout << "Student Information\n";
cout << "Student Name: " << two.FullName << endl;
cout << "Address: " << two.CompleteAddress << endl;
if( two.Gender == 'f' || two.Gender == 'F' )
cout << "Gender: Female" << endl;
else if( two.Gender == 'm' || two.Gender == 'M' )
cout << "Gender: Male" << endl;
else
cout << "Gender: Unknown" << endl;
cout << "Age: " << two.Age << endl;
if( two.LivesInASingleParentHome == true )
cout << "Lives in a single parent home" << endl;
else
cout << "Doesn't live in a single parent home" << endl;
cout << "\n";
return 0;
}
|