|
Microsoft Visual C++/MFC File Processing: MFC's
Standard I/O File Processing |
|
|
The MFC library provides it own version of C's file
processing. This is done through a class named CStdioFile. The
CStdioFile class is derived from CFile.
As done for the FILE structure, to
start normal file input/output operations, declare a variable of type
CStdioFile. This class has five constructors whose syntaxes are:
|
CStdioFile();
CStdioFile(CAtlTransactionManager* pTM);
CStdioFile(FILE* pOpenStream);
CStdioFile(LPCTSTR lpszFileName, UINT nOpenFlags);
CStdioFile(LPCTSTR lpszFileName, UINT nOpenFlags, CAtlTransactionManager* pTM);
The default constructor allows you to initiate file
processing without giving much detail. If you use it, you can then call
the Open() method that the CStdioFile class
inherits from CFile. Pass the same arguments you would for a
CFile variable.
Practical
Learning: Introducing MFC's Standard File Processing
|
|
- To create a new application, on the main menu, click File -> New
Project...
- In the middle list, click MFC Application
- Set the Name to LoanPreparation1
- Click OK
- In the first page of the MFC Application Wizard, click Next
- In the second page of the wizard, click Dialog Box
- Click Next
- In the third page of the wizard, change the Dialog Title to
Loan Preparation
- Click Next twice
- Click Finish
- On the dialog box, click TODO: and press Delete
- Press the OK button and press Delete
- Click the Cancel button to select it
- In the Properties window, click Caption, type Close
and press Enter
- Design the dialog box as follows:
Control |
Caption |
ID |
Right Align Text |
Static Text |
|
Prepared By: |
|
|
Edit Control |
|
|
IDC_CUSTOMERNAME |
|
Static Text |
|
Prepared For: |
|
|
Edit Control |
|
|
IDC_EMPLOYEENAME |
|
Static Text |
|
________________ |
|
|
Static Text |
|
Loan Amount: |
|
|
Edit Control |
|
|
IDC_PRINCIPAL |
True |
Static Text |
|
Interest Rate: |
|
|
Edit Control |
|
|
IDC_INTERESTRATE |
True |
Static Text |
|
% |
|
|
Static Text |
|
Paid In: |
|
|
Edit Control |
|
|
IDC_PERIODS |
True |
Static Text |
|
Months |
|
|
Button |
|
Evaluate |
IDC_EVALUATE |
|
Static Text |
|
_________________ |
|
|
Static Text |
|
Future Value: |
|
|
Edit Control |
|
|
IDC_FUTUREVALUE |
True |
Static Text |
|
Monthly Payment: |
|
|
Edit Control |
|
|
IDC_MONTHLYPAYMENT |
True |
Static Text |
|
_________________ |
|
|
Static Text |
|
Save As: |
|
|
Edit Control |
|
|
IDC_FILESAVE |
True |
Button |
|
Save |
IDC_SAVE_BTN |
|
Static Text |
|
File to Open: |
|
|
Edit Control |
|
|
IDC_FILEOPEN |
True |
Button |
|
Open |
IDC_OPEN_BTN |
|
Static Text |
|
File Name: |
|
|
Button |
|
Reset |
IDC_RESET_BTN |
|
Button |
|
Close |
IDCANCEL |
|
|
- Right-click each edit control, click Add Variable, select the
category as Value, set the options as follows and click Finish on
each:
Control ID |
Category |
Variable Type |
Variable Name |
IDC_CUSTOMERNAME |
Value |
CString |
m_CustomerName |
IDC_EMPLOYEENAME |
Value |
CString |
m_EmployeeName |
IDC_PRINCIPAL |
Value |
CString |
m_Principal |
IDC_INTERESTRATE |
Value |
CString |
m_InterestRate |
IDC_PERIODS |
Value |
CString |
m_Periods |
IDC_FUTUREVALUE |
Value |
CString |
m_FutureValue |
IDC_MONTHLYPAYMENT |
Value |
CString |
m_MonthlyPayment |
IDC_FILESAVE |
Value |
CString |
m_FileSave |
IDC_FILEOPEN |
Value |
CString |
m_FileOpen |
- Access the source file of the dialog box and change the
initializations in the constructor as follows:
CLoanPreparation1Dlg::CLoanPreparation1Dlg(CWnd* pParent /*=NULL*/)
: CDialogEx(CLoanPreparation1Dlg::IDD, pParent)
, m_CustomerName(_T(""))
, m_EmployeeName(_T(""))
, m_Principal(_T("0.00"))
, m_InterestRate(_T("0.00"))
, m_Periods(_T("0"))
, m_FutureValue(_T("0.00"))
, m_MonthlyPayment(_T("0.00"))
, m_FileSave(_T(""))
, m_FileOpen(_T(""))
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
- Return to the dialog box
- Double-click the Evaluate button
- Implement the event as follows:
void CLoanPreparation1Dlg::OnBnClickedEvaluate()
{
UpdateData(TRUE);
double Principal = _wtof(m_Principal);
double InterestRate = _wtof(m_InterestRate) / 100;
double Periods = _wtof(m_Periods) / 12;
double InterestAmount = Principal * InterestRate * Periods;
double FutureValue = Principal + InterestAmount;
double MonthlyPayment = FutureValue / _wtof(m_Periods);
m_FutureValue.Format(L"%.2f", FutureValue);
m_MonthlyPayment.Format(L"%.2f", MonthlyPayment);
UpdateData(FALSE);
}
- Return to the dialog box
- Double-click the Reset button
- Implement the event as follows:
void CLoanPreparation1Dlg::OnBnClickedResetBtn()
{
m_CustomerName = L"";
m_EmployeeName = L"";
m_Principal = L"0.00";
m_InterestRate = L"0.00";
m_Periods = L"0.00";
m_FutureValue = L"0.00";
m_MonthlyPayment = L"0.00";
m_FileSave = L"";
m_FileOpen = L"";
UpdateData(FALSE);
}
Writing to Standard Stream
|
|
Naturally the primary operation you would want to
perform on a file consists of writing one or more values to it. To let you
continually write a string without any concern for the end of line, you
can use the Write() method that the CStdioFile gets from
its parent. If you want the compiler to add a new line at the end of each
written string, you can use the WriteString() method of
the CStdioFile class. Its syntax is:
virtual void WriteString(LPCTSTR lpsz);
|
|