8: Loading and Saving Files

Win32++

Tutorials

Menu of tutorials

Tutorial 1:   The Simplest WindowTutorial 2:   Using Classes and Inheritance
Tutorial 3:   Using Messages to Create a Scribble Window
Tutorial 4:   Repainting the Window
Tutorial 5:   Wrapping a Frame around our Scribble Window
Tutorial 6:   Customising Window Creation
Tutorial 7:   Customising the ToolBar
Tutorial 8:   Loading and Saving Files
Tutorial 9:   Printing
Tutorial 10: Finishing Touches

Tutorial 8:  Loading and Saving Files

In this tutorial we will demonstrate how to save our data to a file, and load it back again.

Saving Data

To save the data we perform the following steps:

  • Use CFile::SaveFileDialog to open a dialog and choose the filename to save the data to.
  • Use CFile::Open to open the file for writing.
  • Use CFile::Write to write the data to the file.

This code segment shows how to use the SaveFileDialog function to choose the file to write to.

void CMainFrame::OnFileSaveAs()
{
  CFile File;
  CString str = 

   
  File.SaveFileDialog(0, OFN_OVERWRITEPROMPT, _T("Save File"), _T("Scribble Files (*.dat)\0*.dat\0\0"), _T("dat"), this);

  // Store the PlotPoint data in the file
  if (!str.IsEmpty())
  {
    m_PathName = str;

    // Save the file name
    m_View.FileSave(str);
    AddMRUEntry(str);
  }
}
This next code segment demonstrates how to open a file for writing, and write data to it. The data written in this case is the contents of the m_points vector. Error checking has been omitted from the code segment displayed here for clarity, but is included in the code for this tutorial.  
BOOL CView::FileSave(LPCTSTR szFilename)
{
  BOOL bResult = TRUE;
  CFile hFile;
  hFile.Open(szFilename, CREATE_ALWAYS))

  // Write the file
for
  (size_t i = 
   
   

     
    0;  i <  m_points.size(); ++i)
  {
    hFile.Write(&m_points[i], sizeof(PlotPoint));
  }

  return bResult;
}

Loading Data

Reading data back from a file follows a rather similar process. The steps involved are as follows:

  • Use CFile::OpenFileDialog to open a dialog and choose the file to load.
  • Use CFile::Open to open the file for reading.
  • Use CFile::Read to retrieve the data from the file.

The following code segment demonstrates how to use OpenFileDialog to choose the file to load.

void CMainFrame::OnFileOpen()
{
  CFile File;
  CString str = 

   
  File.OpenFileDialog(0, OFN_FILEMUSTEXIST, _T("Open File"), _T("Scribble Files (*.dat)\0*.dat\0\0"), this);

  if (!str.IsEmpty())
  {
    // Retrieve the PlotPoint data
    if (m_View.FileOpen(str))
    {
      // Save the filename
      m_PathName = str;
      AddMRUEntry(str);
    }
    else
      m_PathName=_T("");
  }
}

This is the code which loads our PlotPoint vector from the contents of the chosen file. Once again, the error checking has been omitted from the code displayed here for clarity, but is present in the source code. 

BOOL CView::FileOpen(LPCTSTR szFilename)
{
  // empty the PlotPoint vector
  m_points.clear();
  DWORD nBytesRead;
  BOOL bResult = FALSE;

  // Create a handle to the file
  CFile File;
  if (File.Open(szFilename, OPEN_EXISTING))
  {
    do
    {
      nBytesRead = 0;
      PlotPoint pp;

      nBytesRead = File.Read(&pp, sizeof(PlotPoint));
      if (nBytesRead == sizeof(PlotPoint))
        m_points.push_back(pp);	

    } while (nBytesRead == sizeof(PlotPoint));

    bResult = TRUE;
  }

  Invalidate();
  return bResult;
}

The source code for this tutorial is located within the Tutorial folder of the software available from SourceForge at http://sourceforge.net/projects/win32-framework.