MFC库提供其自己的文件处理版本,这是通过名为CStdioFile的类完成的,CStdioFile类派生自CFile,它可以处理Unicode文本文件以及普通的多字节文本文件的读写。
这是构造函数的列表,可以初始化CStdioFile对象-
CStdioFile(); CStdioFile(CAtlTransactionManager* pTM); CStdioFile(FILE* pOpenStream); CStdioFile(LPCTSTR lpszFileName, UINT nOpenFlags); CStdioFile(LPCTSTR lpszFileName, UINT nOpenFlags, CAtlTransactionManager* pTM);
这是CStdioFile中的方法列表-
Sr.No. | Name & 描述 |
---|---|
1 | Open Open设计用于默认的CStdioFile构造函数(重写CFile::Open)。 |
2 | ReadString 读取一行文本。 |
3 | Seek 定位当前文件指针。 |
4 | WriteString 写一行文本。 |
让无涯教程通过创建一个新的基于MFC对话框的应用程序来再次查看一个简单的示例。
步骤1 - 拖动一个编辑控件和两个按钮,如以下快照所示。
步骤2 - 为编辑控件添加值变量 m_strEditCtrl 。
步骤3 - 为"Open"和"Save"按钮添加单击事件处理程序。
步骤4 - 这是事件处理程序的实现。
void CMFCStandardIODlg::OnBnClickedButtonOpen() { //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::OnBnClickedButtonSave() { //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(); }
步骤5 - 编译并执行上述代码后,您将看到以下输出。
步骤6 - 写点东西,然后单击"Save"。它将数据保存在* .txt文件中。
步骤7 - 如果您查看文件的位置,则会看到它包含test.txt文件。
步骤8 -现在,关闭应用程序。运行相同的应用程序。当您单击打开时,将再次加载相同的文本。
步骤9 -首先打开文件,读取文件,然后更新编辑控件。
链接:https://www.learnfk.comhttps://www.learnfk.com/mfc/mfc-standard-io.html
来源:LearnFk无涯教程网
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)