MFC 读写文件
2010-04-23 10:55
232 查看
写入文件:
读取文件:
-------------------------------------------------------------------
举例:
1.先用vc6.0创建个MFC的对话框程序 ReadFile工程,界面如下图:
2.在对话框上右键,建立【类向导】(classwizard),在【成员变量】(Member Variables)中为控件添加关联变量。如下图:
3.在双击【保存】按钮,为点击事件添加代码:
4.双击【查看】按钮,为点击事件添加代码:
CFile file("abc.txt", CFile::modeWrite|CFile::modeCreate); //打开abc.txt文件 CString str = "abcedkwfjwfj"; file.Write(str, str.GetLength()); //写入文件 file.Close();
读取文件:
CFile file("abc.txt", CFile::modeRead); DWORD dwFileLen; dwFileLen = file.GetLength(); char* pBuf; pBuf = new char[dwFileLen+1]; pBuf[dwFileLen] = 0; file.Read(pBuf,dwFileLen); file.Close(); CString csBuf; csBuf.Format("%s", pBuf); //csBuf字符串即为abc.txt中的字符串 MessageBox(csBuf, "", MB_OK);
-------------------------------------------------------------------
举例:
1.先用vc6.0创建个MFC的对话框程序 ReadFile工程,界面如下图:
2.在对话框上右键,建立【类向导】(classwizard),在【成员变量】(Member Variables)中为控件添加关联变量。如下图:
3.在双击【保存】按钮,为点击事件添加代码:
void CReadFileDlg::OnButton1() { // TODO: Add your control notification handler code here SYSTEMTIME stTime; GetLocalTime(&stTime); //获取系统时间 CString date; date.Format("%4d年%2d月%2d日.txt", stTime.wYear, stTime.wMonth, stTime.wDay); //以系统时间作为文件名 CFile file(date, CFile::modeWrite|CFile::modeCreate); CString huanhang = "#"; CString xing = "*"; UpdateData(TRUE); file.Write(m_income, m_income.GetLength()); file.Write(huanhang, huanhang.GetLength()); file.Write(m_pay, m_pay.GetLength()); file.Write(xing, xing.GetLength()); file.Write(m_cash, m_cash.GetLength()); UpdateData(FALSE); file.Close(); }
4.双击【查看】按钮,为点击事件添加代码:
void CReadFileDlg::OnButton2() { // TODO: Add your control notification handler code here SYSTEMTIME stTime; GetLocalTime(&stTime); CString date; date.Format("%4d年%2d月%2d日.txt", stTime.wYear, stTime.wMonth, stTime.wDay); CFile file(date, CFile::modeRead); DWORD dwFileLen; dwFileLen = file.GetLength(); char* pBuf; pBuf = new char[dwFileLen+1]; pBuf[dwFileLen] = 0; file.Read(pBuf,dwFileLen); file.Close(); CString csBuf, cstemp; csBuf.Format("%s", pBuf); int flag1 = csBuf.Find('#', 0); int flag2 = csBuf.Find('*', 0); UpdateData(TRUE); m_income = csBuf.Left(flag1); m_pay = csBuf.Mid(flag1 + 1, flag2-1 - flag1); m_cash = csBuf.Right(csBuf.GetLength()-1 - flag2); UpdateData(FALSE); delete []pBuf; }
相关文章推荐
- MFC API实现Excel读写 屏蔽调用SaveAs函数时弹出“文件已存在”问题
- 【MFC 学习笔记】CFile读写文件
- MFC文件普通读写
- MFC读写txt文件
- MFC CStdioFlie类静态对象对文件的读写操作问题
- MFC读写INI配置文件
- MFC 简单文件读写操作
- MFC中的文件读写技术
- MFC之文件的创建与读写
- 【mfc】利用文件的读写,theApp全局变量来现实登录帐号管理系统
- MFC CFileDialog + 对文件的读写操作
- MFC炼狱之旅(一)文件读写及曲线图
- MFC读写INI配置文件
- MFC中读写文件
- 十一:MFC关于读写TXT文件
- MFC中可用的读写文件操作
- MFC文件读写之ini文件
- MFC中的CFile以及windowsAPI文件读写(转)
- WIN32、MFC、VC、INI文件读写API
- MFC文件读写之ini文件