第十二课

const char *char * const的区别。C语言对文件读写的支持,FILE指针;文本文件和二进制文件的区别。用文本方式读写文件和以二进制方式读写文件的注意事项。C++对文件读写的支持,ofstreamifstream的用法。Win32 SDK对文件读写的支持,CreateFile函数、WriteFile函数、ReadFile函数的使用;MFC对文件读写的支持,CFile类和 CFileDialog的使用,文件过滤器的设置。win.ini文件和注册表的读写方式及相关知识点。

 
  
  1. Ini 文件操作 
  2.  
  3. ::WriteProfileString("http://www.sunxin.org","admin","zhangsan");写入ini,注册表 
  4. CString str; 
  5. ::GetProfileString("http://www.sunxin.org","admin","lisi",str.GetBuffer(100),100);读取ini, 
  6. 注册表 
  7.  
  8. 注册表的操作 
  9. 写入 
  10. WriteProfileString("my ini test","jiajia","i love hf very much"); 
  11. GetProfileString("my ini test","jiajia","lisi"); 
  12.  
  13.  
  14. void CFileView::OnRegWrite()  
  15.     // TODO: Add your command handler code here 
  16.     HKEY hKey; 
  17.     DWORD dwAge=30; 
  18.     RegCreateKey(HKEY_LOCAL_MACHINE,"Software\\http://www.sunxin.org\\admin",&hKey); 
  19.     RegSetValue(hKey,NULL,REG_SZ,"zhangsan",strlen("zhangsan"));        设置缺省项值 
  20.     RegSetValueEx(hKey,"age",0,REG_DWORD,(CONST BYTE*)&dwAge,4);设置注册表项值 
  21.     RegCloseKey(hKey);关闭注册表 
  22.  
  23.  
  24.  
  25. void CFileView::OnRegRead()  
  26.     // TODO: Add your command handler code here 
  27. /*  LONG lValue; 
  28.     RegQueryValue(HKEY_LOCAL_MACHINE,"Software\\http://www.sunxin.org\\admin", 
  29.         NULL,&lValue);读取注册表项缺省值 
  30.     char *pBuf=new char[lValue]; 
  31.     RegQueryValue(HKEY_LOCAL_MACHINE,"Software\\http://www.sunxin.org\\admin", 
  32.         pBuf,&lValue); 
  33.     MessageBox(pBuf);*/ 
  34.     HKEY hKey; 
  35.     RegOpenKey(HKEY_LOCAL_MACHINE,"Software\\http://www.sunxin.org\\admin",&hKey);
  36. 打开注册表 
  37.     DWORD dwType; 
  38.     DWORD dwValue; 
  39.     DWORD dwAge; 
  40.     RegQueryValueEx(hKey,"age",0,&dwType,(LPBYTE)&dwAge,&dwValue); 
  41.     CString str; 
  42.     str.Format("age=%d",dwAge); 
  43.     MessageBox(str); 
  44. }  
 
  
  1. 写入文件 
  2. C语言的方法 
  3. /*  FILE *pFile=fopen("1.txt","w"); 
  4.     fwrite("http://www.sunxin.org",1,strlen("http://www.sunxin.org"),pFile); 
  5.     //fseek(pFile,0,SEEK_SET); 
  6.     //fwrite("ftp:",1,strlen("ftp:"),pFile); 
  7.     //fwrite("http://www.sunxin.org",1,strlen("http://www.sunxin.org"),pFile); 
  8.     fclose(pFile);*/ 
  9.     //fflush(pFile); 
  10.  
  11. /*  FILE *pFile=fopen("2.txt","wb"); 
  12.     char ch[3]; 
  13.     ch[0]='a'; 
  14.     ch[1]=10; 
  15.     ch[2]='b'; 
  16.     fwrite(ch,1,3,pFile); 
  17.     fclose(pFile);*/ 
  18.  
  19.     /*FILE *pFile=fopen("3.txt","w"); 
  20.     int i=98341; 
  21.     char ch[5];*/ 
  22.     /*ch[0]=9+48; 
  23.     ch[1]=8+48; 
  24.     ch[2]=3+48; 
  25.     ch[3]=4+48; 
  26.     ch[4]=1+48;*/ 
  27.     /*itoa(i,ch,10); 
  28.  
  29.     //fwrite(&i,4,1,pFile); 
  30.     fwrite(ch,1,5,pFile); 
  31.     fclose(pFile);*/ 
  32. C++的方法 
  33. /*  ofstream ofs("4.txt"); 
  34.     ofs.write("http://www.sunxin.org",strlen("http://www.sunxin.org")); 
  35.     ofs.close();*/ 
  36.  
  37.  
  38. 读取文件 
  39. C语言的方法 
  40. /*  FILE *pFile=fopen("1.txt","r"); 
  41. //  char ch[100]; 
  42. //  memset(ch,0,100); 
  43. //  fread(ch,1,100,pFile); 
  44. //  MessageBox(ch); 
  45.     char *pBuf; 
  46.     fseek(pFile,0,SEEK_END); 
  47.     int len=ftell(pFile); 
  48.     pBuf=new char[len+1]; 
  49.     rewind(pFile); 
  50.     fread(pBuf,1,len,pFile); 
  51.     pBuf[len]=0; 
  52.     MessageBox(pBuf); 
  53.     fclose(pFile);*/ 
  54. /*  FILE *pFile=fopen("2.txt","rb"); 
  55.     char ch[100]; 
  56.     fread(ch,1,3,pFile); 
  57.     ch[3]=0; 
  58.     MessageBox(ch); 
  59.     fclose(pFile);*/ 
  60. C++的方法 
  61. /*  ifstream ifs("4.txt"); 
  62.     char ch[100]; 
  63.     memset(ch,0,100); 
  64.     ifs.read(ch,100); 
  65.     ifs.close(); 
  66.     MessageBox(ch);*/ 
  67.  
  68.  
  69. Windows api 操作 
  70. /*  HANDLE hFile; 
  71.     hFile=CreateFile("5.txt",GENERIC_READ,0,NULL,OPEN_EXISTING, //创建,打开文件 
  72.         FILE_ATTRIBUTE_NORMAL,NULL); 
  73.     char ch[100]; 
  74.     DWORD dwReads; 
  75.     ReadFile(hFile,ch,100,&dwReads,NULL);                //读取文件  
  76.     ch[dwReads]=0; 
  77.     CloseHandle(hFile); 
  78.     MessageBox(ch);*/ 
  79. /*  CFile file("6.txt",CFile::modeRead); 
  80.     char *pBuf; 
  81.     DWORD dwFileLen; 
  82.     dwFileLen=file.GetLength(); 
  83.     pBuf=new char[dwFileLen+1]; 
  84.     pBuf[dwFileLen]=0; 
  85.     file.Read(pBuf,dwFileLen); 
  86.     file.Close(); 
  87.     MessageBox(pBuf);*/ 
  88.  
  89.  
  90. /*  HANDLE hFile; 
  91.     hFile=CreateFile("5.txt",GENERIC_WRITE,0,NULL,CREATE_NEW, 
  92.         FILE_ATTRIBUTE_NORMAL,NULL); 
  93.     DWORD dwWrites; 
  94.     WriteFile(hFile,"http://www.sunxin.org",strlen("http://www.sunxin.org"),//写入文件 
  95.         &dwWrites,NULL); 
  96.     CloseHandle(hFile);*/ 
  97. /*  CFile file("6.txt",CFile::modeCreate | CFile::modeWrite); 
  98.     file.Write("http://www.sunxin.org",strlen("http://www.sunxin.org")); 
  99.     file.Close();*/ 
  100.  
  101.  
  102. 通用控件的文件操作 
  103.  
  104.     CFileDialog fileDlg(FALSE); 
  105.     fileDlg.m_ofn.lpstrTitle="我的文件保存对话框"
  106.     fileDlg.m_ofn.lpstrFilter="Text Files(*.txt)\0*.txt\0All Files(*.*)\0*.*\0\0"
  107.     fileDlg.m_ofn.lpstrDefExt="txt"
  108.     if(IDOK==fileDlg.DoModal()) 
  109.     { 
  110.         CFile file(fileDlg.GetFileName(),CFile::modeCreate | CFile::modeWrite); 
  111.         file.Write("http://www.sunxin.org",strlen("http://www.sunxin.org")); 
  112.         file.Close(); 
  113.     } 
  114.  
  115.  
  116.  
  117.     CFileDialog fileDlg(TRUE); 
  118.     fileDlg.m_ofn.lpstrTitle="我的文件打开对话框"
  119.     fileDlg.m_ofn.lpstrFilter="Text Files(*.txt)\0*.txt\0All Files(*.*)\0*.*\0\0"
  120.      
  121.     if(IDOK==fileDlg.DoModal()) 
  122.     { 
  123.         CFile file(fileDlg.GetFileName(),CFile::modeRead); 
  124.         char *pBuf; 
  125.         DWORD dwFileLen; 
  126.         dwFileLen=file.GetLength(); 
  127.         pBuf=new char[dwFileLen+1]; 
  128.         pBuf[dwFileLen]=0; 
  129.         file.Read(pBuf,dwFileLen); 
  130.         file.Close(); 
  131.         MessageBox(pBuf); 
  132.     }