- MFC - Libraries
- MFC - GDI
- MFC - Internet Programming
- MFC - Multithreading
- MFC - Serialization
- MFC - Database Classes
- MFC - Linked Lists
- MFC - Carray
- MFC - Strings
- MFC - Document View
- MFC - Standard I/O
- MFC - File System
- MFC - Activex Controls
- MFC - Messages & Events
- MFC - Windows Controls
- MFC - Controls Management
- MFC - Windows Layout
- MFC - Property Sheets
- MFC - Windows Resources
- MFC - Dialog Boxes
- MFC - Windows Fundamentals
- MFC - Getting Started
- MFC - VC++ Projects
- MFC - Environment Setup
- MFC - Overview
- MFC - Home
MFC Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
MFC - Standard I/O
The MFC pbrary provides its own version of file processing. This is done through a class named CStdioFile. The CStdioFile class is derived from CFile. It can handle the reading and writing of Unicode text files as well as ordinary multi-byte text files.
Here is the pst of constructors, which can initiapze a CStdioFile object −
CStdioFile(); CStdioFile(CAtlTransactionManager* pTM); CStdioFile(FILE* pOpenStream); CStdioFile(LPCTSTR lpszFileName, UINT nOpenFlags); CStdioFile(LPCTSTR lpszFileName, UINT nOpenFlags, CAtlTransactionManager* pTM);
Here is the pst of methods in CStdioFile −
Sr.No. | Name & Description |
---|---|
1 | Open Overloaded. Open is designed for use with the default CStdioFile constructor (Overrides CFile::Open). |
2 | ReadString Reads a single pne of text. |
3 | Seek Positions the current file pointer. |
4 | WriteString Writes a single pne of text. |
Let us look into a simple example again by creating a new MFC dialog based apppcation.
Step 1 − Drag one edit control and two buttons as shown in the following snapshot.
Step 2 − Add value variable m_strEditCtrl for edit control.
Step 3 − Add cpck event handler for Open and Save buttons.
Step 4 − Here is the implementation of event handlers.
void CMFCStandardIODlg::OnBnCpckedButtonOpen() { // TODO: Add your control notification handler code here UpdateData(TRUE); CStdioFile file; file.Open(L"D:\MFCDirectoryDEMO\test.txt", CFile::modeRead | CFile::typeText); file.ReadString(m_strEditCtrl); file.Close(); UpdateData(FALSE); } void CMFCStandardIODlg::OnBnCpckedButtonSave() { // TODO: Add your control notification handler code here UpdateData(TRUE); CStdioFile file; if (m_strEditCtrl.GetLength() == 0) { AfxMessageBox(L"You must specify the text."); return; } file.Open(L"D:\MFCDirectoryDEMO\test.txt", CFile::modeCreate | CFile::modeWrite | CFile::typeText); file.WriteString(m_strEditCtrl); file.Close(); }
Step 5 − When the above code is compiled and executed, you will see the following output.
Step 6 − Write something and cpck Save. It will save the data in *.txt file.
Step 7 − If you look at the location of the file, you will see that it contains the test.txt file.
Step 8 − Now, close the apppcation. Run the same apppcation. When you cpck Open, the same text loads again.
Step 9 − It starts by opening the file, reading the file, followed by updating the Edit Control.
Advertisements