孙鑫第12课笔记——文件和注册表的操作
2010-11-08 15:48
281 查看
孙鑫第12课笔记——文件和注册表的操作
1.MFC中用CFile来完成对文件的操作CFile类:派生于CObject类
构造函数:
CFile( );
CFile( int hFile );//该参数为一个已打开文件的句柄
CFile( LPCTSTR lpszFileName, UINT nOpenFlags );//常用
lpszFileName:文件名或路径
nOpenFlags:常用的打开方式
CFile::modeCreate 不存在则创建新文件,存在则将文件长度截取为0;
CFile::modeNoTruncate 与modeCreate连用,不存在则创建新文件,存在也不会把文件长度截取为0;
CFile::modeRead Opens the file for reading only.
CFile::modeReadWrite Opens the file for reading and writing.
CFile::modeWrite Opens the file for writing only.
在菜单中添加读取文件和写入文件两项后,在CFileView中实现这两个操作:
void CFileView::OnFileread()
{
CFile file("6.txt",CFile::modeRead);//注意这里不要再用modeCreate,否则写入的内容会被清空
DWORD dwFileLength=file.GetLength();
char* pBuf=new char[dwFileLength+1];
pBuf[dwFileLength]=0;//字符串尾,否则读出的是乱码
file.Read(pBuf,dwFileLength);
file.Close();
MessageBox(pBuf);
}
void CFileView::OnFilewrite()
{
// TODO: Add your command handler code here
CFile file("6.txt",CFile::modeCreate|CFile::modeWrite);
file.Write("L12-MFC-file",strlen("L12-MFC-file"));
file.Close();
}
2.CFileDialog类
CFileDialog类可以完成“打开”或“另存为”对话框的创建。可以通过它的成员函数获得文件的名字、存储类型、存储路径等外部信息,但并不能获得文件内容的相关信息,因此,往往把CFileDialog获得的外部信息作为参数传递给CFile对象,再由CFile对象对文件内容进行操作。
CFileDialog操作的一般步骤是:
1. 创建一个CFileDialog对象,并利用构造器初始化它
2. 如果需要对对话框的某些属性进行设置,可以修改CFileDialog的成员变量m_ofn中的相关属性,该变量的类型为OPENFILENAME
3. 调用Domodal,if(IDOK==fileDlg.DoModal()){此处加入对文件的操作,一般是利用CFile对象}
CFileDialog中常用的几个函数:
DoModal | 不用多说吧 |
GetPathName | 获得全路径名 |
GetFileName | 获得文件名(包括类型,如.txt) |
GetFileExt | 获得文件类型. |
GetFileTitle | 获得文件标题(不包括类型). |
{
CFileDialog fileDlg(TRUE);
fileDlg.m_ofn.lpstrTitle="我的文件打开对话框";
fileDlg.m_ofn.lpstrFilter="Text Files(*.txt)/0*.txt/0All Files(*.*)/0*.*/0/0";//这里注意过滤器的格式
if(IDOK==fileDlg.DoModal())
{
CFile file(fileDlg.GetFileName(),CFile::modeRead);
char *pBuf;
DWORD dwFileLen;
dwFileLen=file.GetLength();
pBuf=new char[dwFileLen+1];
pBuf[dwFileLen]=0;
file.Read(pBuf,dwFileLen);
file.Close();
MessageBox(pBuf);
}
}
void CFileView::OnFilewrite()
{
CFileDialog fileDlg(FALSE);
fileDlg.m_ofn.lpstrTitle="我的文件保存对话框";
fileDlg.m_ofn.lpstrFilter="Text Files(*.txt)/0*.txt/0All Files(*.*)/0*.*/0/0";
fileDlg.m_ofn.lpstrDefExt="txt";
if(IDOK==fileDlg.DoModal())
{
CFile file(fileDlg.GetFileName(),CFile::modeCreate | CFile::modeWrite);
file.Write("http://www.sunxin.org",strlen("http://www.sunxin.org"));
file.Close();
}
}
3.注册表编程
void CFileView::OnRegWrite()
{
// TODO: Add your command handler code here
HKEY hKey;
DWORD dwAge=30;
RegCreateKey(HKEY_LOCAL_MACHINE,"Software//http://www.sunxin.org//admin",&hKey);
RegSetValue(hKey,NULL,REG_SZ,"zhangsan",strlen("zhangsan"));
RegSetValueEx(hKey,"age",0,REG_DWORD,(CONST BYTE*)&dwAge,4);
RegCloseKey(hKey);
}
void CFileView::OnRegRead()
{
// TODO: Add your command handler code here
/* LONG lValue;
RegQueryValue(HKEY_LOCAL_MACHINE,"Software//http://www.sunxin.org//admin",
NULL,&lValue);//第三个参数为NULL表示得到所要读取的数据长度
char *pBuf=new char[lValue];
RegQueryValue(HKEY_LOCAL_MACHINE,"Software//http://www.sunxin.org//admin",
pBuf,&lValue);
MessageBox(pBuf);*/
HKEY hKey;
RegOpenKey(HKEY_LOCAL_MACHINE,"Software//http://www.sunxin.org//admin",&hKey);
DWORD dwType;
DWORD dwValue;
DWORD dwAge;
RegQueryValueEx(hKey,"age",0,&dwType,(LPBYTE)&dwAge, &dwValue);
CString str;
str.Format("age=%d",dwAge);
MessageBox(str);
}
相关文章推荐
- 《VC++深入详解》学习笔记---12章文件和注册表操作(1)
- C#学习笔记21——文件、目录、注册表操作
- 孙鑫VC学习笔记:第十二讲 用API函数、CFile类操作文件
- 《VC++深入详解》学习笔记---12章文件和注册表操作(2)
- 第12课 文件和注册表操作
- (C#)学习笔记1:文件和注册表操作
- 孙鑫-MFC笔记七--文件与注册表
- MFC - 文件操作 (孙鑫C++第十二讲笔记整理)
- 孙鑫VC学习笔记:第十二讲 (四) 用API函数、CFile类操作文件
- 孙鑫:第十二讲 文件、注册表、文件对话框的操作
- 孙鑫VC视频教程笔记之第十二课“文件操作(含注册表操作)”
- Windows文件及注册表操作学习笔记…
- C对文件的一些操作(孙鑫vc++的阅读笔记)
- 12月20日笔记 文件、目录权限及相关操作命令
- VC操作文件、注册表
- 孙鑫VC学习笔记:第十二讲 用C++函数读写文件
- Linux复习笔记(二) -- Linux文件系统基本结构和操作
- Windows phone 8 学习笔记(2) 数据文件操作
- 实例介绍文件下载,注册表操作等基本技巧!(原创)
- iOS学习笔记(十七)——文件操作(NSFileManager)