VC6.0编的“文件读写”的两种方式(MFC和基于对话框)
软件实验三:基于对话框的MFC程序实现文件读写操作 实例一:
操作步骤:
1、 新建“MFC APPWIZARD(EXE)”,选择文件路径,输入文件名,选择应用程序类型为“基本对话框”,按上图添加各控件
2、 按下表修改各控件的ID号,添加成员函数和消息函数 控件 标题 ID号 IDC_WRITE 成员变量 变量类型 消息函数 编辑框 (写入的文本) 编辑框(文件路径) 编辑框(从文件中读入的文本) 下压按钮 写入到文件 下压按钮 查看文件路径 下压按钮 从文件读入的文本 m_WriteString CString IDC_FILEPATH m_FilePath CString IDC_READ m_strRead Cstring IDC_BUTTON_WRITE OnButtonWrite IDC_BUTTON_FILEPATH OnButtonFilepath IDC_BUTTON_READ OnButtonRead 给控件添加成员变量的方法为:按ctrl+w,进入MFC ClassWizard,选中要添加成员变量的控件的ID号,选择mumber variables面板,单击add variable按钮,添加成员变量,并选择变量类型。
3、 写入文件
1)在“基于对话框的文件读写Dlg.h”文件中定义变量(粗体字为此处添加的代码) class CMyDlg : public CDialog {
// Construction public: //定义变量
CString strFileName;//文件名 CString strFilePath;//文件路径
BOOL IsTextFile(CString& rFile);//判断文件类型是否正确
CMyDlg(CWnd* pParent = NULL); ………
// standard constructor
}
2)双击下压按钮“写入文件”,编写“写入文件”的消息处理函数 void CMyDlg::OnButtonWrite() //单击“写入到文件”按钮 {
// TODO: Add your control notification handler code here UpdateData(true); CFile file;
CFileDialog filedlg(0,//1-文件打开,0-文件另存为
\".txt|*.*\NULL,
OFN_OVERWRITEPROMPT,
\"文本文件(*.txt)|*.txt|All Files(*.*)|*.*||\NULL);
if(filedlg.DoModal()==IDOK) strFileName=filedlg.GetFileName();//获得文件名 if(strFileName==\"\") {
AfxMessageBox(\"请输入文件名\");
return; }
file.Open(strFileName,CFile::modeCreate|CFile::modeWrite);//将数据写入文件 int length=m_WriteString.GetLength();//获取文件长度
file.Write((LPCTSTR)m_WriteString,length);//获取有关文件的信息CString AfxMessageBox(\"已保存到文件:\"+strFileName+\"!\");//保存结束提示 }
4、 查看文件路径
双击下压按钮“查看文件路径”,编写“查看文件路径”的消息处理函数 void CMyDlg::OnButtonFilepath() //查看文件路径 { }
// TODO: Add your control notification handler code here m_FilePath=strFilePath; UpdateData(false);
strFilePath=file.GetFilePath();//获得文件的路径 file.Close();//关闭文件
5、 读入文件
1) 定义判断文件类型是否正确的函数(粗体字为此处添加的代码) class CMyDlg : public CDialog {
// Construction public: //定义变量
CString strFileName;//文件名 CString strFilePath;//文件路径
BOOL IsTextFile(CString& rFile);//判断文件类型是否正确 CMyDlg(CWnd* pParent = NULL); ………
}
2) 编写IsTextFile函数
BOOL CMyDlg::IsTextFile(CString &rFile)//判断文件类型是否正确 {
CStringList strList; CString str(rFile);
strList.AddHead(\".TXT\"); strList.AddHead(\".SYS\"); strList.AddHead(\".BAT\");
strList.AddHead(\".DAT\");
str=str.Right(4);//文件名的右边四位字符 str.MakeUpper();//转换成大写
// standard constructor
return(strList.Find(str))?TRUE:FALSE;
}
3) 在“基于对话框的文件读写Dlg.cpp”文件开头添加头文件 #include \"fstream.h\"//添加的头文件 4) 单击下压按钮“读入文件”,编写下压按钮“读入文件”的消息函数 void CMyDlg::OnButtonRead() {
// TODO: Add your control notification handler code here fstream f1;//定义文件流对象
char s[200];
CFileDialog filedlg(1,//1-文件打开,0-文件另存为
\".txt|*.*\
NULL,
OFN_OVERWRITEPROMPT,
\"文本文件(*.txt)|*.txt|All Files(*.*)|*.*||\NULL);
if(filedlg.DoModal()==IDOK) { UpdateData(true); strFileName=filedlg.GetFileName();//获得文件名 if(!IsTextFile(strFileName))//判断文件类型是否正确 }
6、 运行程序
1) 在写入文件的编辑框中输入内容,单击“写入文件”按钮,并可选择路径保存该文件。 2) 单击“查看文件路径”,可查看该文件所要路径。 3) 单击“从文件读入”按钮,可选择一个文件并打开,并把文件内容显示在从文件读入的
文本编辑框中。
}
{ }
AfxMessageBox(\"文件类型不正确\"); return;
f1.open(strFileName,ios::in|ios::nocreate); while(!f1.eof()) {
f1.getline(s,255);
m_strRead=m_strRead+\"\\r\\n\"+s;//添加文件中的文本到编辑框
UpdateData(false); }
AfxMessageBox(strFileName+\"文件读入完毕\");//保存结束提示 f1.close();//关闭文件流
实例二:(老师的例子)
操作步骤:
1、 新建“MFC APPWIZARD(EXE)”,选择文件路径,输入文件名,选择应用程序类型为“基
本对话框”,按上图添加各控件
2、 修改各控件的ID号,为各控件添加成员变量和消息函数 控件 标题 ID号 成员变量 变量类型 消息函数 编辑框 IDC_EDIT_FILEDATA 按钮 Read IDC_BTN_READ_FILE 按钮 Write IDC_BTN_WRITE_FILE ctrFileData OnBtnReadFile() OnBtnWriteFile() 给控件添加成员变量的方法为:按ctrl+w,进入MFC ClassWizard,选中要添加成员变量的控件的ID号,选择mumber variables面板,单击add variable按钮,添加成员变量,并选择变量类型。
3、双击按钮“Read”,添加“Read”的消息函数 void CMFCDlg::OnBtnReadFile() //读文件 {
// TODO: Add your control notification handler code here
CFileDialog Filedlg( TRUE, \"All file\, \"All File (*.*)\");
Filedlg.m_ofn.lpstrTitle = \"打开文件\"; CString m_strFileName; CString m_strFileExm; if( Filedlg.DoModal() != IDOK ) return; else {
m_strFileName = Filedlg.GetPathName(); //被打开文件名
m_strFileExm = Filedlg.GetFileExt(); //被打开文件扩展名
CFile cFile;
CFileException e;
if ( cFile.Open( m_strFileName, CFile::modeReadWrite, &e ) )// If open file successful
{
unsigned long lSize = cFile.GetLength (); char *pBuf = new char[lSize+1]; if ( !pBuf ) // Out of memory { } else {
// Alloc memory success return ;
cFile.ReadHuge ( pBuf, lSize );
*( pBuf + lSize ) = '\\0' ; m_ctrFileData.SetWindowText( pBuf );
if ( pBuf ) // Release memory delete []pBuf;
// Close file manually
}
cFile.Close ();
} else {
// End of if ( cFile.Opne...)
TCHAR szCause[255]; CString strFormatted;
e.GetErrorMessage( szCause, 255 );
strFormatted = _T(\"The data file could not be read because of this error: \"); strFormatted += szCause;
AfxMessageBox( strFormatted ); }
}
}
return ;
4、双击按钮“Write”,添加“Write”的消息函数 void CMFCDlg::OnBtnWriteFile() //写文件 {
// TODO: Add your control notification handler code here
CFileDialog Filedlg( FALSE, \"All file\, \"All File (*.*)\");
Filedlg.m_ofn.lpstrTitle = \"写文件\"; CString m_strFileName; CString m_strFileExm; CString m_strFileData;
CFile cFile;
CFileException e; unsigned long lSize; if( Filedlg.DoModal() != IDOK )
else {
m_strFileName = Filedlg.GetPathName(); //被打开文件名
if ( cFile.Open( m_strFileName, CFile::modeCreate | CFile::modeWrite, &e ) )// If open return;
m_strFileExm = Filedlg.GetFileExt(); //被打开文件扩展名
file successful {
m_ctrFileData.GetWindowText( m_strFileData ); lSize = m_strFileData.GetLength(); char *pBuf = new char[lSize+1]; if ( !pBuf ) // Out of memory {
cFile.Close (); return ;
} else // Alloc memory success { strcpy( pBuf, (char *)(LPCSTR)m_strFileData );
*( pBuf + lSize ) = '\\0' ;
cFile.WriteHuge( pBuf,lSize );
} else {
}
if ( pBuf ) // Release memory delete []pBuf;
cFile.Close (); // Close file manually // End of if ( cFile.Opne...)
TCHAR szCause[255];
CString strFormatted;
e.GetErrorMessage( szCause, 255 );
strFormatted = _T(\"The data file could not be read because of this error: \"); strFormatted += szCause; AfxMessageBox( strFormatted ); }
}
}
return ;
5、运行程序
1)单击“Read”按钮可选择并打开一个文件,并把文件内容显示在编辑框中 2)可在编辑框中输入一些信息,单击“Write”,可以把信息保存为一个文件。