FunctionX - Practical Learning Logo

MFC File Processing

 

Introduction to File Processing

 

Overview

 

File processing is the ability to create, store, and/or retrieve the contents of a file from a medium such as a hard drive, a floppy disk, a CD-ROM, or a DVD-ROM, etc. One of the areas in which the MFC excels is file processing. MFC makes it particularly easy to perform most file related operations.

 

What to Process, When, and Where

Before creating a file, you should first plan your program since applications deal with different types of files. An application that manipulate pictures may need to save only one value for the whole file. Another application used for word processing may also need to save only one value but of a completely different kind. If an application is list-based, it may need to save various values that, when put together, constitute one file.

After deciding what type of file the user would be processing, you would also specify how and/or who would decide when to process a file. For example, if you create a certain application that allows a user to change the internal settings and store them in a file, you can process the file automatically and store it somewhere without asking the user to decide when, where, or how to save the file (some applications use the Registry to take care of this). For example, in tthe bits of data that are arranged in a particular way to produce a usable document. For easy storage, location, and management, the bits are stored on a medium such as a hard disc, a floppy disc, a compact disc, or any valid and support type of storage. When these bits belong to a single but common entity, the group is referred to as a file. For even greater management, files can be stored in a parent object called a directory or a folder. Since a file is a unit of storage and it stores information, it has a size which is the number of bits it contains. To manage it, a file also has a location also called a path that specifies where and/or how the file can be retrieved. Also, for better management, a file has attributes that indicate what can be done on a file or that provide specific information that the programmer or the operating system can use when dealing with the file.

In Jasc Paint Shop Pro, users are allowed to create new customized gradients that were not installed with the application. Once a user clicks OK, the new gradient is saved (but where?... who cares?):

Another way you can deal with this is to let a user decide when to save the file and where to save it. This is usually taken care of by providing the File Dialog to the user.

 

Archiving

 

Introduction

Most of the file processing in an MFC application is performed in conjunction with a class called CArchive. The CArchive class serves as a relay between the application and the medium used to either store data or make it available. The CArchive class is an accessory and not an end to itself. As such, it provides its services to the MFC class that needs to perform file transfer.

Although used by MFC, the CArchive class is semi-independent. That is, it is not derived from CObject. It only provides its services to the MFC objects that need archiving. To provide this support, the parent class of MFC objects, CObject, is equipped with a method called Serialize(). The CObject::Serialize() method takes one argument, which is a CArchive reference variable. The syntax of the CObject::Serialize() method is:

virtual void Serialize(CArchive& ar);

This means that the MFC library already provides file processing at a very high level in the library's hierarchy. This also implies that almost every MFC class is ready to save its value(s) from a medium or to retrieve (an) existing value(s) from a medium. This convenience is given at a very low price, as file processing is one of MFC strengths. Based on this, to provide file processing for an object in your application, you can simply override the Serialize() method.

 

The Process of Archiving

One of the most regular ways you will use the CArchive class consists of storing or retrieving values through a Serialize() method. To do this directly and easily in the class, the CArchive class overloads two operators: << and >>.

To store one or more values, the CArchive class provides the << operator. The values you can store are of MFC, BYTE, short, WORD, int, unsigned int, long, LONG, DWORD, float, or double types. The class also provides an exception handler for each kind. Therefore, the syntaxes for this operator are:

friend CArchive& operator <<( CArchive& ar, const CObject* pOb );
throw( CArchiveException, CFileException );
CArchive& operator <<( BYTE by );
throw( CArchiveException, CFileException );
CArchive& operator <<( WORD w );
throw( CArchiveException, CFileException );
CArchive& operator <<( int i );
throw( CArchiveException, CFileException );
CArchive& operator <<( LONG l );
throw( CArchiveException, CFileException );			
CArchive& operator <<( DWORD dw );
throw( CArchiveException, CFileException );
CArchive& operator <<( float f );
throw( CArchiveException, CFileException );
CArchive& operator <<( double d );
throw( CArchiveException, CFileException );

As you may have realized, one version of the Serialize() member function is called once for both storing and retrieving. Therefore, when using it, you must specify what operation you want to perform. This checking process can be performed by calling the CArchive::IsStoring() Boolean method. Its syntax is:

BOOL IsStoring() const;

This method returns TRUE if you are storing data. If the answer is then TRUE, you can use the << operator to store each necessary value.

As opposed to storing value(s), to retrieve a value, the CArchive class overloads the >> operator. It comes in the following syntaxes:

friend CArchive& operator >>( CArchive& ar, CObject *& pOb );
throw( CArchiveException, CFileException, CMemoryException );
friend CArchive& operator >>( CArchive& ar, const CObject *& pOb );
throw( CArchiveException, CFileException, CMemoryException );
CArchive& operator >>( BYTE& by );
throw( CArchiveException, CFileException );
CArchive& operator >>( WORD& w );
throw( CArchiveException, CFileException );
CArchive& operator >>( int& i );
throw( CArchiveException, CFileException );
CArchive& operator >>( LONG& l );
throw( CArchiveException, CFileException );
CArchive& operator >>( DWORD& dw );
throw( CArchiveException, CFileException );
CArchive& operator >>( float& f );
throw( CArchiveException, CFileException );
CArchive& operator >>( double& d );
throw( CArchiveException, CFileException );

This indicates that you can retrieve an MFC object, a BYTE, a short, a WORD, an int, an unsigned int, a long, a LONG, a DWORD, a float, or a double value.

As mentioned above, the Serialize() method is used for both operations. Therefore, when using this method, you should first check what operation is being performed. This can be done by calling the IsLoading() method. Its syntax is:

BOOL IsLoading() const;

This method returns TRUE if you are retrieving a value or some values. If it is true, you can then use the >> operator to retrieve the value(s).

Practical Learning: Introducing File Processing

  1. If you want to follow this exercise, create a new MFC Application
  2. Name it Palace Ice Cream
  3. Change the design of the IDR_MAINFRAME icon as follows:
     
     
  4. Design the dialog box as follows:
     
     
    Control ID Caption Additional Properties
    Static Text Static Text   First Name:  
    Edit Box IDC_FIRST_NAME    
    Static Text Static Text   Last Name:  
    Edit Box Edit Box IDC_LAST_NAME    
    Static Text Static Text   Address:  
    Edit Box IDC_ADDRESS    
    Static Text Static Text   City:  
    Edit Box IDC_CITY    
    Static Text Static Text   State:  
    Edit Box IDC_STATE  
    Static Text Static Text   ZIP Code:  
    Edit Box IDC_ZIP_CODE    
    Static Text Static Text   Hourly Salary:  
    Edit Box IDC_HOURLY_SALARY    
    Group Box   Employment Status  
    Radio Button Radio Button IDC_PART_TIME Part Time Group: True
    Left Text: True
    Radio Button Radio Button IDC_FULL_TIME Full Time Left Text: True
    Button Button IDOK    
    Button Button IDCANCEL    
  5. Right-click each control whose ID was specified and click Add Member Variable
  6. Using the Add Member Variable Wizard, add the following member variables to the controls:
     
    Identifier Value Variable
    IDC_FIRST_NAME CString m_FirstName
    IDC_LAST_NAME CString m_LastName
    IDC_ADDRESS CString m_Address
    IDC_CITY CString m_City
    IDC_STATE CString m_State
    IDC_ZIP_CODE long m_ZIPCode
    IDC_HOURLY_SALARY double m_HourlySalary
    IDC_PART_TIME int m_EmploymentStatus
  7. Save everything 
 

MFC Support of File Processing

 

Introduction

MFC supports file processing at different levels. There is so much file processing that one of your biggest difficulties would consist of deciding what file processing system you want to use. MFC supports C, C++, Win32, and MFC file processing. Keep in mind that, as we orderly listed the systems, each is backward compatible. That is, C++ supports C and C++ file processing. Win32 supports C, C++ and its own Win32 file processing. MFC supports C, C++, Win32, and its own MFC file processing. Even if you select only one of these categories, some of them have other variances. For example, the C++ language provides more than one way to perform file processing. MFC itself is tremendously wide on this issue.

One of the classes used to perform file processing on MFC applications is CFile. Using this class, to create a file, you can first declare a variable of type CFile using one of its 3 constructors. They are;

CFile( );
CFile( int hFile );
CFile( LPCTSTR lpszFileName, UINT nOpenFlags );

The default constructor is used if you are not ready to specify the information needed to create the file.

The second constructor takes an argument that is a handle to a file created using the Win32's CreateFile() function.

The third constructor is used to create a file by specifying the file name and the flags that specify the operation to perform when saving or opening the file.

 

Automatic File Processing

Automatic File Processing consists of saving or opening a file with little or no intervention from the user. This technique is used to provide values that should be automatically provided to the user. This technique also allows the user to create or add a new value to a list that is deemed incomplete.

Practical Learning: Using the File Dialog Box

  1. Assuming that the user would have typed information about an employee, to automatically save the file, double-click the OK button
  2. Accept the suggested name of the event and implement it as follows:
     
    void CPalaceIceCreamDlg::OnOK() 
    {
    	// TODO: Add extra validation here
    	// TODO: Add your control notification handler code here
    	this->UpdateData();
    
    	CFile flEmployees;
    
    	flEmployees.Open("employees.mpl", CFile::modeCreate | CFile::modeWrite);
    	CArchive ar(&flEmployees, CArchive::store);
    
    	ar << m_FirstName << m_LastName << m_Address << m_City << m_State << m_ZIPCode << m_HourlySalary << m_EmploymentStatus;
    		
    	ar.Close();
    	flEmployees.Close();
    
    	CDialog::OnOK();
    }
  3. Execute the application and fill the dialog with some information
     
  4. Click OK to close the dialog box
  5. To automatically load the saved file when the dialog box comes up, generate the WM_CREATE message for the dialog box and implement its OnCreate event as follows:
     
    int CPalaceIceCreamDlg::OnCreate(LPCREATESTRUCT lpCreateStruct) 
    {
    	if (CDialog::OnCreate(lpCreateStruct) == -1)
    		return -1;
    	
    	// TODO: Add your specialized creation code here
    	CFile flEmployees;
    
    	flEmployees.Open("employees.mpl", CFile::modeRead);
    	CArchive ar(&flEmployees, CArchive::load);
    
    	ar >> m_FirstName >> m_LastName >> m_Address >> m_City >> m_State >> m_ZIPCode >> m_HourlySalary >> m_EmploymentStatus;
    	
    	ar.Close();
    	flEmployees.Close();
    
    	return 0;
    }
  6. Execute the application
  7. Close it and return to MSVC

 

File Processing Prompt 

The process we used above is used if you don't want the user to participate in saving or opening a file. In many applications, you will leave it up to the user to decide when and where to save a file or when and from where to open a file. This can be done by displaying the File Dialog box.

When a user decides to save or open a file, you can use a CFileDialog variable and call the CFileDialog::GetFileName() method to get the name of the file that the user is dealing with.

 

Practical Learning: Using the File Dialog Box

  1. Add two new buttons to the bottom section of the dialog box as follows:
     
      
    Control ID Caption
    Button IDC_SAVE_BTN Save
    Button IDC_OPEN_BTN Open
    Button IDC_RESET_BTN Reset
  2. Save all
  3. Double-click the Save button and accept the suggested name of the file
  4. Double-click the Open button and accept the suggested name of the file
  5. Double-click the Reset button and accept the suggested name of the file
  6. Implement both events as follows:
     
    // Palace Ice CreamDlg.cpp : implementation file
    //
    
    #include "stdafx.h"
    #include "Palace Ice Cream.h"
    #include "Palace Ice CreamDlg.h"
    
    #ifdef _DEBUG
    #define new DEBUG_NEW
    #undef THIS_FILE
    static char THIS_FILE[] = __FILE__;
    #endif
    
    /////////////////////////////////////////////////////////////////////////////
    // CPalaceIceCreamDlg dialog
    
    CPalaceIceCreamDlg::CPalaceIceCreamDlg(CWnd* pParent /*=NULL*/)
    	: CDialog(CPalaceIceCreamDlg::IDD, pParent)
    {
    	//{{AFX_DATA_INIT(CPalaceIceCreamDlg)
    	m_FirstName = _T("");
    	m_LastName = _T("");
    	m_Address = _T("");
    	m_City = _T("");
    	m_State = _T("");
    	m_ZIPCode = 0;
    	m_HourlySalary = 0.0;
    	m_EmploymentStatus = 0;
    	//}}AFX_DATA_INIT
    	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
    }
    
    void CPalaceIceCreamDlg::DoDataExchange(CDataExchange* pDX)
    {
    	CDialog::DoDataExchange(pDX);
    	//{{AFX_DATA_MAP(CPalaceIceCreamDlg)
    	DDX_Text(pDX, IDC_FIRST_NAME, m_FirstName);
    	DDX_Text(pDX, IDC_LAST_NAME, m_LastName);
    	DDX_Text(pDX, IDC_ADDRESS, m_Address);
    	DDX_Text(pDX, IDC_CITY, m_City);
    	DDX_Text(pDX, IDC_STATE, m_State);
    	DDX_Text(pDX, IDC_ZIP_CODE, m_ZIPCode);
    	DDX_Text(pDX, IDC_HOURLY_SALARY, m_HourlySalary);
    	DDX_Radio(pDX, IDC_PART_TIME, m_EmploymentStatus);
    	//}}AFX_DATA_MAP
    }
    
    BEGIN_MESSAGE_MAP(CPalaceIceCreamDlg, CDialog)
    	//{{AFX_MSG_MAP(CPalaceIceCreamDlg)
    	ON_WM_PAINT()
    	ON_WM_QUERYDRAGICON()
    	ON_BN_CLICKED(IDC_SAVE_BTN, OnSaveBtn)
    	ON_BN_CLICKED(IDC_OPEN_BTN, OnOpenBtn)
    	ON_BN_CLICKED(IDC_RESET_BTN, OnResetBtn)
    	//}}AFX_MSG_MAP
    END_MESSAGE_MAP()
    
    /////////////////////////////////////////////////////////////////////////////
    // CPalaceIceCreamDlg message handlers
    
    . . .
    
    
    
    void CPalaceIceCreamDlg::OnSaveBtn() 
    {
    	// TODO: Add your control notification handler code here
    	this->UpdateData();
    
    	CFile flEmployees;
    	char strFilter[] = { "Employees Records (*.mpl)|*.mpl|Palace Ice Cream Files (*.pis)|*.pis|All Files (*.*)|*.*||" };
    
    	CFileDialog FileDlg(FALSE, ".mpl", NULL, 0, strFilter);
    
    	if( FileDlg.DoModal() == IDOK )
    	{
    		if( flEmployees.Open(FileDlg.GetFileName(), CFile::modeCreate | CFile::modeWrite) == FALSE )
    			return;
    		CArchive ar(&flEmployees, CArchive::store);
    
    		ar << m_FirstName << m_LastName << m_Address << m_City << m_State << m_ZIPCode << m_HourlySalary << m_EmploymentStatus;
    		ar.Close();
    	}
    	else
    		return;
    
    	flEmployees.Close();
    }
    
    void CPalaceIceCreamDlg::OnOpenBtn() 
    {
    	// TODO: Add your control notification handler code here
    	CFile flEmployees;
    	char strFilter[] = { "Employees Records (*.mpl)|*.mpl|Palace Ice Cream Files (*.pis)|*.pis|All Files (*.*)|*.*||" };
    
    	CFileDialog FileDlg(TRUE, ".mpl", NULL, 0, strFilter);
    	
    	if( FileDlg.DoModal() == IDOK )
    	{
    		if( flEmployees.Open(FileDlg.GetFileName(), CFile::modeRead) == FALSE )
    			return;
    		CArchive ar(&flEmployees, CArchive::load);
    
    		ar >> m_FirstName >> m_LastName >> m_Address >> m_City >> m_State >> m_ZIPCode >> m_HourlySalary >> m_EmploymentStatus;
    		ar.Close();
    	}
    	else
    		return;
    
    	flEmployees.Close();
    
    	UpdateData(FALSE);
    }
    
    void CPalaceIceCreamDlg::OnResetBtn() 
    {
    	// TODO: Add your control notification handler code here
    	this->m_FirstName.Format("");
    	this->m_LastName.Format("");
    	this->m_Address.Format("");
    	this->m_City.Format("");
    	this->m_State.Format("");
    	this->m_ZIPCode = 0;
    	this->m_HourlySalary = 10.15;
    	this->m_EmploymentStatus = 1;
    
    	this->UpdateData(FALSE);
    }
  7. Test the application

Document/View Architecture File Processing

The MFC library provides a more advanced and elaborate system of file processing in the internals of the document/view architecture through the CDocument class. Although this system is not the easiest and is not shared by other programming environments, once you are familiar with it, you may find it convenient.

In the Document/View architecture, a file or its content is displayed in the view while its real estate is managed by the frame. Therefore, while using a document, the user typically uses the view to manipulate the file. The trick for you is when it becomes time to save the file. Since you shouldn't manipulate the document in the document class (?), when it is time to save it, you must "transfer" the values to be saved from the view to the document class. Then let the document class handle the processing of the file. This is valid both for saving and opening the file.

Practical Learning: Processing Files in Document/View

  1. Start a new application as Single Document and name it LoanEval
  2. Base its view class on CFormView
  3. Design the form as follows:
     
    Control ID Caption Additional Properties
    Group Box Group Box   Loan Preparation  
    Static Text Static Text   Prepared By:  
    Edit Box IDC_PREPARED_BY    
    Static Text Static Text   Prepared For:  
    Edit Box IDC_PREPARED_FOR    
    Group Box Group Box   Values  
    Static Text Static Text   Principal:
    Edit Box IDC_PRINCIPAL   Align Text: Right
    Static Text   Interest:
    Edit Box IDC_INTEREST_RATE   Align Text: Right
    Static Text   %  
    Static Text   Periods:  
    Edit Box IDC_PERIODS   Align Text: Center
    Number: True
    Static Text   Years  
    Group Box   Compound Frequency  
    Radio Button IDC_COMPOUND Monthly Group: True
    Left Text: True
    Radio Button   Quarterly Left Text: True
    Radio Button   Semiannually Left Text: True
    Radio Button   Annually Left Text: True
    Group Box      
    Static Text   Interest Earned  
    Edit Box IDC_INTEREST_TOEARN   Align Text: Right
    Static Text   Amount Earned  
    Edit Box IDC_FUTURE_VALUE   Align Text: Right
    Button IDC_CALCULATE_BTN Calculate Default Button: True
    Button IDC_RESET_BTN Reset  
  4. Using the Add Member Variable Wizard, add the following member variables to the controls:
     
    Identifier Type Value Variable
    IDC_PREPARED_BY CString m_PreparedBy
    IDC_PREPARED_FOR CString m_PreparedFor
    IDC_PRINCIPAL double m_Principal
    IDC_INTEREST_RATE double m_InterestRate
    IDC_PERIODS UINT m_Periods
    IDC_COMPOUND int m_Compound
    IDC_INTEREST_TOEARN double m_InterestToEarn
    IDC_FUTURE_VALUE double m_FutureValue
  5. Change the design of the IDR_MAINFRAME icon as follows:
     
  6. In the header file of the document class, declare a variable for each value that will be used in file processing:
     
    // LoanEvalDoc.h : interface of the CLoanEvalDoc class
    //
    /////////////////////////////////////////////////////////////////////////////
    
    . . .
    
    
    class CLoanEvalDoc : public CDocument
    {
    protected: // create from serialization only
    	CLoanEvalDoc();
    	DECLARE_DYNCREATE(CLoanEvalDoc)
    
    // Attributes
    public:
    	CString    strEmployee;
    	CString	strCustomer;
    	double	Principal;
    	double	InterestRate;
    	UINT        Periods;
    	int	Compound;
    
    // Operations
    public:
    
    . . .
                  
    };
    
    . . .
                     
  7. To prepare the file for processing, access the Serialize() event of the document and change it as follows:
     
    void CLoanEvalDoc::Serialize(CArchive& ar)
    {
    	if (ar.IsStoring())
    	{
    		ar << strEmployee << strCustomer <<	Principal << InterestRate << Periods <<	Compound;
    	}
    	else
    	{
    		ar >> strEmployee >> strCustomer >> Principal >> InterestRate >> Periods >> Compound;
    	}
    }
  8. Access the StdAfx.h header file and include the math.h header file:
     
    . . .
                    
    #define VC_EXTRALEAN		// Exclude rarely-used stuff from Windows headers
    
    #include <afxwin.h>         // MFC core and standard components
    #include <afxext.h>         // MFC extensions
    #include <afxdisp.h>        // MFC Automation classes
    #include <afxdtctl.h>		// MFC support for Internet Explorer 4 Common Controls
    #ifndef _AFX_NO_AFXCMN_SUPPORT
    #include <afxcmn.h>			// MFC support for Windows Common Controls
    #endif // _AFX_NO_AFXCMN_SUPPORT
    
    #include <math.h>
    
    . . .
    
    
  9. Generate a BN_CLICKED message for the the IDC_CALCULATE_BTN control associated with the view class and accept the suggested name of the function
  10. Implement it as follows:
     
    void CLoanEvalView::OnCalculateBtn() 
    {
    	// TODO: Add your control notification handler code here
    	UpdateData();
    	double InterestToEarn;
    	double FutureValue;
    	int CompoundType;
    	
    	switch( m_Compound )
    	{
    	case 0:
    		CompoundType = 12;
    		break;
    	case 1:
    		CompoundType = 4;
    		break;
    	case 2:
    		CompoundType = 2;
    		break;
    	case 3:
    		CompoundType = 1;
    		break;
    	}
    	
    	double i = (m_InterestRate / 100) / CompoundType;
    	int n    = CompoundType * m_Periods;
    
    	FutureValue    = m_Principal * pow(1 + i, n);
    	InterestToEarn = FutureValue - m_Principal;
    
    	CWnd *edtInterestToEarn = reinterpret_cast<CEdit *>(GetDlgItem(IDC_INTEREST_TOEARN));
    	CWnd *edtFutureValue    = reinterpret_cast<CEdit *>(GetDlgItem(IDC_FUTURE_VALUE));
    
    	char InterestAmount[20], FValue[20];
    	sprintf(InterestAmount, "%.2f", InterestToEarn);
    	sprintf(FValue, "%.2f", FutureValue);
    
    	edtInterestToEarn->SetWindowText(InterestAmount);
    	edtFutureValue->SetWindowText(FValue);
    }
  11. Generate a message for the ID_FILE_SAVE menu item associated with the view class and accept the suggested name of the function
  12. Implement it as follows:
     
    void CLoanEvalView::OnFileSave() 
    {
    	// TODO: Add your command handler code here
    	this->UpdateData();
    	CLoanEvalDoc *pDoc = GetDocument();
    
    	pDoc->strEmployee  = this->m_PreparedBy;
    	pDoc->strCustomer  = this->m_PreparedFor;
    	pDoc->Principal    = this->m_Principal;
    	pDoc->InterestRate = this->m_InterestRate;
    	pDoc->Periods      = this->m_Periods;
    	pDoc->Compound     = this->m_Compound;
    
    	CFile flLoan;
    
    	char strFilter[] = { "Loan Files (*.onf)|*.onf|Text Files (*.txt)|*.txt|All Files (*.*)|*.*||" };
    
    	CFileDialog FileDlg(FALSE, ".bcr", NULL, 0, strFilter);
    
    	if( FileDlg.DoModal() == IDOK )
    	{
    		flLoan.Open(FileDlg.GetFileName(), CFile::modeCreate | CFile::modeWrite);
    		CArchive ar(&flLoan, CArchive::store);
    		pDoc->Serialize(ar);
    
    		ar.Close();
    	}
    	else
    		return;
    
    	flLoan.Close();
    }
  13. Generate a message for the ID_FILE_OPEN menu item associated with the view class and accept the suggested name of the function
  14. Implement it as follows:
     
    void CLoanEvalView::OnFileOpen() 
    {
    	// TODO: Add your command handler code here
    	UpdateData();
    	CLoanEvalDoc *pDoc = GetDocument();
    
    	CFile flLoan;
    
    	char strFilter[] = { "BCR Files (*.onf)|*.onf|Text Files (*.txt)|*.txt|All Files (*.*)|*.*||" };
    
    	CFileDialog FileDlg(FALSE, ".bcr", NULL, 0, strFilter);
    
    	
    	if( FileDlg.DoModal() == IDOK )
    	{
    		if( flLoan.Open(FileDlg.GetFileName(), CFile::modeRead) == FALSE )
    			return;
    		CArchive ar(&flLoan, CArchive::load);
    		pDoc->Serialize(ar);
    
    		m_PreparedBy.Format("%s", pDoc->strEmployee);
    		m_PreparedFor.Format("%s", pDoc->strCustomer);
    		m_Principal    = pDoc->Principal;
    		m_InterestRate = pDoc->InterestRate;
    		m_Periods      = pDoc->Periods;
    		m_Compound     = pDoc->Compound;
    
    		ar.Close();
    	}
    	else
    		return;
    
    	flLoan.Close();
    	UpdateData(FALSE);
    }
  15. Access the frame's source file to remove the default title and set your own as follows:
     
    // MainFrm.cpp : implementation of the CMainFrame class
    //
    
    . . .
    
    /////////////////////////////////////////////////////////////////////////////
    // CMainFrame construction/destruction
    
    . . .
    
    int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
    {
    	if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
    		return -1;
    	
    	. . .
    	
    	this->SetWindowText("Loan Evaluation");
    	this->CenterWindow();
    
    	return 0;
    }
    
    BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
    {
    	if( !CFrameWnd::PreCreateWindow(cs) )
    		return FALSE;
    
    	cs.style &= ~FWS_ADDTOTITLE;
    
    	return TRUE;
    }
    
    /////////////////////////////////////////////////////////////////////////////
    // CMainFrame diagnostics
    
    . . .
    
  16. Execute the application then create and save a record
     

MFC File Processing Options

In the above example, we used the CFile class to discuss file processing. MFC provides more extended file processing support than that. In reality, file processing is one of the strengths of MFC. It excels in it by providing the following classes:

FILE: FILE is a C structure used to perform file processing for that language. You can also use it in your MFC applications

CStdioFile: MFC provides the same file processing support as the C language through the CStdioFile class

Win32: The Win32 library provides its own mechanism of file processing through its HANDLE concept. A file is created using the CreateFile() function.

 


Copyright © 2004-2010 FunctionX, Inc.