孙鑫第12课笔记——文件和注册表的操作
来源:互联网 发布:java远程调用技术 编辑:程序博客网 时间:2024/05/29 19:06
孙鑫第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
获得文件标题(不包括类型).
void CFileView::OnFileread()
{
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); }
- 孙鑫第12课笔记——文件和注册表的操作
- 《VC++深入详解》学习笔记[9]——第12章 文件和注册表操作
- 第12课 文件和注册表操作
- C#学习笔记21——文件、目录、注册表操作
- 文件和注册表操作
- 12文件和注册表操作(1)
- 第12篇 文件和注册表操作
- (C#)学习笔记1:文件和注册表操作
- 第十二讲__文件的操作和注册表的操作
- 《VC++深入详解》学习笔记---12章文件和注册表操作(1)
- 《VC++深入详解》学习笔记---12章文件和注册表操作(2)
- 读写操作,文件和注册表
- 孙鑫VC++第12章文件和注册表操作
- UAC文件和注册表操作虚拟化
- (二十四)文件和注册表操作
- 文件和注册表操作(C#)
- C#高级编程-文件和注册表操作
- 《VC++深入详解》学习笔记 第十二章 文件和注册表操作
- 数据结构二分法
- 临时
- 使用Form个性化修改标准Form的LOV
- [UML] MVC的运作模式
- BI Publisher:限制每页打印的行数(Page Breaks)
- 孙鑫第12课笔记——文件和注册表的操作
- 实现IPv6:KAME项目的经验
- !!!
- [BusyBox] "Using fallback suid method" Message
- java unicode转换代码
- mysq比较时间
- 网易邮箱登陆链接
- JS控制文本框的disabled问题
- Java方法