Can anybody help me!! I am learning Visual C++ 6. I am trying to learn how to use C++ streams to store/retrieve variables of differing data types into/from a single binary file.
A book which I have got about VC++ 6.0 mentions "Serialization of application". In it, you create an AppWizard application giving your application name "Serialize". The application is a SDI application using Document/View architecture where the view class is based on CFormView class.
I have created my own custom class called CPerson (a generic class) with its base class CObject so that I can hold the class in an object array. After that, I have made the class serializable as follows:
class CPerson: public COject
{ DECLARE_SERIAL (CPerson)
private: int m_iAge; CString m_sName;
public:
void Serialize(CArchive &ar) {
CObject::Serialize(ar);
if (ar.IsStoring()) ar << m_sName << m_iAge;
else ar >> m_sName >> m_iAge;
}
};
Then in the header for the document class (i.e. SerializeDoc.h), I have declared the variables as follows:
class SerializeDoc: public CDocument
{ private: CObArray m_oaPeople; int m_iCurPosition;
public:
void Serialize(CArchive &ar) { //Pass the serialization on to the object array
m_oaPeople.Serialize(ar);
}
};
My application creates CPerson objects in the document (object) and add them to the object array as follows:
CPerson *pPerson = new CPerson();
:
m_oaPeople.Add(pPerson);
:
With a SDI or MDI application, the functionality to save, open a file and exit the window is located on the application menu. So it seems that I do not have to write such functionlity to do them myself.
Question)
Is there a way to give a file a name myself (a binary file), open/create it and then save or retrieve variables of different data types into or from it, without relying on the application menus? Like fopen(), fclose(), fread(), fwrite() and so on as in C? What I mean is "How can I open/create/read/write/close a file in VC++ as we do in C using streams?" How can I save or retrieve the data into or from a single binary file, if I disable the application menus (in a SDI application) which allow me to open or save a file?
Enter your message below
Sign in or Join us (it's free).