Help:C++ streams to access a binary file

  • 12 years ago

    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;


                  void Serialize(CArchive &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;


                   void   Serialize(CArchive &ar) { //Pass the serialization on to the object array





    My application creates CPerson objects in the document (object) and add them to the object array as follows:

             CPerson  *pPerson = new CPerson();




    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.


    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?



  • 12 years ago

    Hi Chong,

    This approach, uses 'CArchive' and 'CFile' classes (you don't need to include anything). An example will show that all :

    • Save to a file :


    CFile f1( "c:\\test.dat", CFile::modeCreate | CFile::modeWrite );

    CArchive ar1( &f1, CArchive::store );


    int num = 58;

    CString str( "String" ); ar1 << num << str;





    • Load from a file :


    f1.Open (
    "c:\\test.dat", CFile::modeRead );

    CArchive ar2( &f1, CArchive::load );


    int num;

    CString str;

    ar2 >> num >> str;





    • Remove/Rename a file :

    You can use static functions of class 'CFile' to remove/rename files :

    CFile::Remove( "c:\\test.dat" );

    CFile::Rename( "c:\\old.dat", "c:\\new.dat" );

  • 12 years ago

    Hi my friend Mohammad

    Thanks for helping me once again.  I need your advice once again.  Using the instructions which you have given me, I have wrriten a code to read a file.  The code is below!!  I am wondering  if there are other ways to read a file!!  If you do know them, could you let me know.

    All the best




    //To read a file to the end of it 

    CFile rfile("test.dat",CFile::modeRead);

    CArchive ar(&rfile, CArchive::load);

    CString sName[SomeLargeNumber];

    int iAge[SomeLargeNumber];

    int i = 0;

    try {

         while (TRUE) {

              ar >> sName[i] >> iAge[i++];

         } // while

    } // try


    catch (CArchiveException *e) {

         if (e->m_cause == CArchiveException::endOfFile) {

              AfxMessageBox("File has been read");

         } else

              AfxMessageBox("Something is wrong");

         e->Delete( );

    }  //catch

    ar.Close( );

    rfile.Close( );




  • 12 years ago

    Please read my article :

    File Handling in VC++6

    Great Times! Wink

Post a reply

Enter your message below

Sign in or Join us (it's free).


Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“PHP is a minor evil perpetrated and created by incompetent amateurs, whereas Perl is a great and insidious evil perpetrated by skilled but perverted professionals.” - Jon Ribbens