1.我们要做的一个demo是创建文本文件并进行读写数据的操作
Cfile类是mfc中文件操作的积累,他派生自CObject,直接提供二进制文件的输入输出服务。本例将介绍如何使用CFile类进行将编辑框中的文本储存好txt文件中,并可以进行文件的读取操作。
写入文件的代码如下:
void CMFCApplication1Dlg::OnBnClickedButton1()
{
// TODO: 写入文件的代码
UpdateData(TRUE);
CString strFilter, fileName, strText;
strFilter = "Text Files(*.txt)|*.txt|";
CFileDialog dlg(FALSE, NULL, NULL, OFN_EXPLORER | OFN_HIDEREADONLY | OFN_ENABLESIZING | OFN_FILEMUSTEXIST, strFilter);
if (dlg.DoModal() == IDOK){
fileName = dlg.GetPathName();
CFile saveFile(fileName, CFile::modeCreate | CFile::modeWrite);
saveFile.Write(m_strWrite, m_strWrite.GetLength());
saveFile.Close();
}
}
读取文件的代码如下:
void CMFCApplication1Dlg::OnBnClickedButton2()
{
// TODO: 读取文件的代码
CString strFilter, fileName, strText;
strFilter = "Text Files(*.txt)|*.txt|";
CFileDialog dlg(TRUE, NULL, NULL, OFN_EXPLORER | OFN_HIDEREADONLY | OFN_ENABLESIZING | OFN_FILEMUSTEXIST, strFilter);
if (dlg.DoModal() == IDOK){
fileName = dlg.GetPathName();
CFile openfile(fileName, CFile::modeRead);
int lenght = openfile.GetLength();
char *strTEXT;
strTEXT = new char[lenght];
openfile.Read(strTEXT, lenght);
openfile.Close();
m_strRead = strTEXT;
delete[]strTEXT;
UpdateData(FALSE);
}
}
注意:在使用CFile类的时候,要特别小心文件存取模式耳朵设置。如本例中的,保存文件,文件是可写的,如果文件存在,会将原来的文件清空。
而在打开文件的时候,不能具有mocecreate标识,否则读取文件会被清空。
2. 在文件的指定位置存放和读取数据
先给出实例的demo
保存按钮的代码如下:
void CMFCApplication2Dlg::OnBnClickedButton2()
{
// TODO: 保存
CString strFilter, fileName, strText;
strFilter = "List Files(*.list)|*.list|";
CFileDialog dlg(FALSE, NULL, NULL, OFN_EXPLORER | OFN_HIDEREADONLY |
OFN_ENABLESIZING | OFN_FILEMUSTEXIST, strFilter);
if (dlg.DoModal() == IDOK){
fileName = dlg.GetPathName();
CFile savefile(fileName, CFile::modeCreate | CFile::modeWrite);
for (int i = 0; i < m_ctllist.GetCount(); i++){
m_ctllist.GetText(i, strText);
savefile.Seek(40 * i, CFile::begin);
savefile.Write(strText, 40);
}
savefile.Close();
}
}
载入代码:
void CMFCApplication2Dlg::OnBnClickedButton3()
{
// TODO: 载入
CString strFilter, fileName;
char * strText;
strFilter = "List Files(*.list)|*.list|";
strText = new char[40];
CFileDialog dlg(TRUE, NULL, NULL, OFN_EXPLORER | OFN_HIDEREADONLY |
OFN_ENABLESIZING | OFN_FILEMUSTEXIST, strFilter);
if (dlg.DoModal() == IDOK){
fileName = dlg.GetPathName();
CFile openfile(fileName, CFile::modeRead);
int i = 0;
openfile.SeekToBegin();
while (openfile.Read(strText, 40) != 0){
m_ctllist.AddString((CString)strText);
i++;
openfile.Seek(40 * i, CFile::begin);
}
openfile.Close();
}
}
注意: 本段代码中有一些值得注意的地方
1.如何实现每次读取的数据恰好是书名呢? 解决方案:采用指针来实现,让插入数据的时候,不管字符长度如何,均占用40个字节,读取的时候每次也读取40个字节,那样就实现了将完整的数据保存和添加到列表框中。
2.出了这个方法还有没别的方法来实现呢?可以采用特征字符的方法,每次插入后都自动插入一个分隔符,从而达到辨别数据有效字段的效果。
3.文件的复制 ,删除,移动
要实现这几个常用的功能,需要使用win 32API中的相关函数。
函数原型如下:
BOOL CopyFile(
LPCTSTR lpExistingFileName, // pointer to name of an existing file
LPCTSTR lpNewFileName, // pointer to filename to copy to
BOOL bFailIfExists // flag for operation if file exists
);
其中各参数的意义:
LPCTSTR lpExistingFileName, // 你要拷贝的源文件名
LPCTSTR lpNewFileName, // 你要拷贝的目标文件名
BOOL bFailIfExists // 如果目标已经存在,不拷贝(True)并返回False,覆盖目标(false)
用例
1
|
MoveFile(
"C:\\File1.txt"
,
"C:\\File3.txt"
);
|
函数功能
参数说明
返回值
这几个函数由于比较简单,就不在赘述了。
4. 实现遍历目录下的每个文件
由于博客的长度过于长,所以在以下就只给出核心代码了
遍历文件的代码如下:
void CMFCApplication4Dlg::OnBnClickedButton2()
{
// TODO: 遍历文件的代码
CString oldDir;
UpdateData(TRUE);
GetCurrentDirectoryW(50, (LPTSTR)(LPCTSTR)oldDir); //获取当前路径
if (!SetCurrentDirectory((LPCTSTR)m_stredit) || m_stredit.IsEmpty()) //如果目录为空
{
m_stredit = (LPCTSTR)oldDir;
UpdateData(FALSE);
}
while (m_filelist.GetCount() != 0){
int index = m_filelist.GetTopIndex();
m_filelist.DeleteString(index);
}
FindFile(m_stredit);
UpdateData(FALSE);
SetCurrentDirectory((LPCTSTR)oldDir);
}
void CMFCApplication4Dlg::FindFile(CString curstr){
//遍历文件中的子函数
HANDLE hfile; //查找文件句柄
WIN32_FIND_DATA wfdata; //文件结构信息
BOOL IsOver = FALSE;
CString strname; //文件名
CString strfull; //全路径
hfile = FindFirstFile((LPCTSTR)("*.*"), &wfdata);
if (hfile == INVALID_HANDLE_VALUE){
IsOver = TRUE;
}
while (!IsOver){
strname.Format(_T("%s"), wfdata.cFileName);
strfull = curstr + "\\" + strname;
if ((wfdata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) && (wfdata.cFileName[0] != TEXT('.'))){
SetCurrentDirectory(wfdata.cFileName);
FindFile(strfull); //递归调用
SetCurrentDirectory(_T("..")); //返回当前目录
}
else if (wfdata.cFileName[0] != TEXT('.')){
m_filelist.AddString(strfull);
IsOver = !FindNextFile(hfile, &wfdata);
}
FindClose(hfile);
}
}