![]() |
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:
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:
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). |
|
|
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. |
|
|
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. |
|
![]() |
||||||||||||
|
// 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); } |
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. |
|
|
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. |
|