第十二课
const char *与char * const的区别。C语言对文件读写的支持,FILE指针;文本文件和二进制文件的区别。用文本方式读写文件和以二进制方式读写文件的注意事项。C++对文件读写的支持,ofstream和ifstream的用法。Win32 SDK对文件读写的支持,CreateFile函数、WriteFile函数、ReadFile函数的使用;MFC对文件读写的支持,CFile类和 CFileDialog的使用,文件过滤器的设置。win.ini文件和注册表的读写方式及相关知识点。
- Ini 文件操作
- ::WriteProfileString("http://www.sunxin.org","admin","zhangsan");写入ini,注册表
- CString str;
- ::GetProfileString("http://www.sunxin.org","admin","lisi",str.GetBuffer(100),100);读取ini,
- 注册表
- 注册表的操作
- 写入
- WriteProfileString("my ini test","jiajia","i love hf very much");
- GetProfileString("my ini test","jiajia","lisi");
- 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);读取注册表项缺省值
- 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);
- }
- 写入文件
- C语言的方法
- /* FILE *pFile=fopen("1.txt","w");
- fwrite("http://www.sunxin.org",1,strlen("http://www.sunxin.org"),pFile);
- //fseek(pFile,0,SEEK_SET);
- //fwrite("ftp:",1,strlen("ftp:"),pFile);
- //fwrite("http://www.sunxin.org",1,strlen("http://www.sunxin.org"),pFile);
- fclose(pFile);*/
- //fflush(pFile);
- /* FILE *pFile=fopen("2.txt","wb");
- char ch[3];
- ch[0]='a';
- ch[1]=10;
- ch[2]='b';
- fwrite(ch,1,3,pFile);
- fclose(pFile);*/
- /*FILE *pFile=fopen("3.txt","w");
- int i=98341;
- char ch[5];*/
- /*ch[0]=9+48;
- ch[1]=8+48;
- ch[2]=3+48;
- ch[3]=4+48;
- ch[4]=1+48;*/
- /*itoa(i,ch,10);
- //fwrite(&i,4,1,pFile);
- fwrite(ch,1,5,pFile);
- fclose(pFile);*/
- C++的方法
- /* ofstream ofs("4.txt");
- ofs.write("http://www.sunxin.org",strlen("http://www.sunxin.org"));
- ofs.close();*/
- 读取文件
- C语言的方法
- /* FILE *pFile=fopen("1.txt","r");
- // char ch[100];
- // memset(ch,0,100);
- // fread(ch,1,100,pFile);
- // MessageBox(ch);
- char *pBuf;
- fseek(pFile,0,SEEK_END);
- int len=ftell(pFile);
- pBuf=new char[len+1];
- rewind(pFile);
- fread(pBuf,1,len,pFile);
- pBuf[len]=0;
- MessageBox(pBuf);
- fclose(pFile);*/
- /* FILE *pFile=fopen("2.txt","rb");
- char ch[100];
- fread(ch,1,3,pFile);
- ch[3]=0;
- MessageBox(ch);
- fclose(pFile);*/
- C++的方法
- /* ifstream ifs("4.txt");
- char ch[100];
- memset(ch,0,100);
- ifs.read(ch,100);
- ifs.close();
- MessageBox(ch);*/
- Windows api 操作
- /* HANDLE hFile;
- hFile=CreateFile("5.txt",GENERIC_READ,0,NULL,OPEN_EXISTING, //创建,打开文件
- FILE_ATTRIBUTE_NORMAL,NULL);
- char ch[100];
- DWORD dwReads;
- ReadFile(hFile,ch,100,&dwReads,NULL); //读取文件
- ch[dwReads]=0;
- CloseHandle(hFile);
- MessageBox(ch);*/
- /* CFile file("6.txt",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);*/
- /* HANDLE hFile;
- hFile=CreateFile("5.txt",GENERIC_WRITE,0,NULL,CREATE_NEW,
- FILE_ATTRIBUTE_NORMAL,NULL);
- DWORD dwWrites;
- WriteFile(hFile,"http://www.sunxin.org",strlen("http://www.sunxin.org"),//写入文件
- &dwWrites,NULL);
- CloseHandle(hFile);*/
- /* CFile file("6.txt",CFile::modeCreate | CFile::modeWrite);
- file.Write("http://www.sunxin.org",strlen("http://www.sunxin.org"));
- file.Close();*/
- 通用控件的文件操作
- 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();
- }
- 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);
- }