孫鑫VC++視頻學習筆記之12:文件操作

轉自:http://webbery.tianyablog.com

  閱讀本文前,我們假設您已經:
   1,知道如何創建一個單文檔的App Wizard
   2,知道C++ 類、函數重載等簡單知識
   3,知道如何給View類或者Doc文檔添加成員變量
   4,會用MFC的IDE調試工具最好,那麼本文的程序您可以copy去調試
   5,知道如何爲某個框架類添加虛函數或消息處理函數
  
  
  1, 指向常量的指針&&指針常量
  
  Char ch[5]=”lisi”;
  Const char * pStr=ch; const char *等同char const *
  Char * const *pStr=ch; 指針是常量,指針不可更改,其內容可更改
  
  2, 讀寫
  
  文件讀取操作
   FILE *pFile=fopen("1.txt","r");
  
   char ch[100]="0"; //數組被賦值爲全零
  memset(ch,0,100);//等同於上一句?
  
   //char ch[100]; //如果不把數組賦零,也可以在寫入文件中多寫一個空字符
   如fwrite("I Love You",1,strlen("I Love You")+1,pFile);
   //memset(ch,0,100); 把數組賦爲全零的另一種方法。
   fread(ch,1,100,pFile);
   fflush(pFile);
  
  3, 獲取文件大小
  
  fseek(pFile,0,SEEK_END); //把文件指針移到文件末尾
   int n=ftell(pFile); //得到文件長度
  
  rewind(pFile); //把指針移回文件頭 fseek(pFile,0,SEEK_BEGIN)
   pbuf=new char[n+1];
   pbuf[n]=0; //文件尾加/0作爲文件結束符
   fread(pbuf,1,n,pFile);
  
  4, 文本和二進制方式。讀取和寫入的保持一致
  文本:寫入, 換行(10)è回車--換行(ASCII爲13、10)
   讀取, 回車--換行(ASCII 13、10)è換行(10)
  
  二進制:將數據在內存中的存儲形式原樣輸出到文件中
  不管是文本文件還是二進制文件,都可以用文本方式或者二進制方式中的任意一種打開
  
  5, 字符和數字
  
  FILE *pFile=fopen("2.txt","w");
  
  int i=98341; //非要使他可用,可增加itoa(i,ch,10);
  fwrite(&i,4,1,pFile);
  
  6, C++中文件操作
  
  需要加頭文件#include "fstream.h"
   ofstream os("3.txt");
   os.write("I love you!",strlen("I love you!"));
   os.close();
  
  讀文件:
  
   ifstream ifs("3.txt",ios::in);
   char ch[100]="0";
  memset(ch,0,100)
   ifs.read(ch,100);
   ifs.close();
  
  //循環讀取文件每一行
  
  While(!ifs.getline(ch,100).eof())
  
  {//do something with data in buffer ch
  
  }
  下一次重新getline之前,需要 ifs.clear()清除eof標誌
  
  7, Win32API函數存取文件
  
  (1)寫入
  HANDLE hfile;
   hfile=CreateFile("6.txt",GENERIC_WRITE,0,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
   DWORD dwWrites;
   WriteFile(hfile,"I Love You!(5)",strlen("I Love You!(5)"),&dwWrites,NULL);
   CloseHandle(hfile);
  (2)讀取
   HANDLE hfile;
   hfile=CreateFile("6.txt",GENERIC_READ,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
   char ch[100]="0"; //如果只寫char ch[dwRead-1];可以在之後用ch[dwRead]=0設結束符
   DWORD dwRead;
   ReadFile(hfile,ch,100,&dwRead,NULL);
  ch[dwRead]=0;
   CloseHandle(hfile);
  
  8, SDK方法
  
  (1)寫入:
  
  CFile file("7.txt",CFile::modeCreate | CFile::modeWrite);
   file.Write("I Love You 1000",strlen("I Love You 1000"));
   file.Close();
  
  (2)讀取:
  
  CFile file("7.txt",CFile::modeRead);
   char *pBuf;
   DWORD i=file.GetLength();
  
  pBuf=new char[i+1];
   file.Read(pBuf,i);
  
  pBuf[i]=0;
  
   file.Close();
  
  9, 構造文件對話框,存取文件方法
  
  (1)寫入:
  
  CFileDialog fileDlg(FALSE);
   fileDlg.m_ofn.lpstrTitle="我的文件保存對話框";
   fileDlg.m_ofn.lpstrFilter="Text Files(*.txt)/0*.txt/0All Files(*.*)/0*.*/0/0";
  //注意lpstrFilter的構造:每個段落後邊都要加/0,末尾要加兩個/0,括號裏的只是顯示,實//現在緊跟着的/0後邊,此過濾器只爲過濾可見文件使用,並不能按所見格式保存。
   fileDlg.m_ofn.lpstrDefExt="txt";
   if(IDOK==fileDlg.DoModal())
   {
   CFile file(fileDlg.GetFileName(),CFile::modeCreate | CFile::modeWrite);
   file.Write("I Love You 1000",strlen("I Love You 1000"));
   file.Close();
   }
  
  (2)讀取
  
   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 i=file.GetLength();
   pbuf=new char[i+1]; //動態建立緩衝區,值得學習
   pbuf[i]=0;
   file.Read(pbuf,i);
   MessageBox(pbuf);
   file.Close();
  
  }
  
  10, 讀寫配置文件
  
  CXXXApp::InitInstance(){
  
  // 寫在SetRegistryKey(_T("Local AppWizard-Generated Applications"));之後(也可以重新設置表項)
  
  ::WriteProfileString("songpeng","sp","song");用來在C:/WINDOWS/win.ini中寫入數據。一方面爲了兼容十六位程序,另一方面提高程序運行速度
   //在win32中爲[HKEY_CURRENT_USER]è[Software]è[Local Appwizard-Generated Applications]è[File]
  
   CString str;
   ::GetProfileString("songpeng","sp","peng",str.GetBuffer(100),100);
  
  }
  
  11, 讀寫註冊表
  
   讀寫配置文件的函數WriteProfileString(),GetProfileString()在win32下自動成爲註冊表的讀寫。
  
    SetRegistryKey(_T("Local AppWizard-Generated Applications"));用來在註冊表的HKEY_CURRENT_USER->Software下增加主鍵Local AppWizard-Generated Applications
  子鍵及值由WriteProfileString("songpeng","sp","song");增加
  
   (1)寫入註冊表:
  
   HKEY hkey;
    RegCreateKey(HKEY_LOCAL_MACHINE,"Software//MyItem//Name",&hkey); RegSetValue(hkey,NULL,REG_SZ,"song",strlen("song"));
   RegSetValueEx(hKey,”age”,0,REG_DWORD,(CONST BYTE*)&dwAge,4);
  
   RegCloseKey(hkey);
  
   (2)讀取註冊表
  
   注意要先獲取字段大小
  
   LONG lValue;
   RegQueryValue(HKEY_LOCAL_MACHINE,"Software//MyItem//Name",NULL,&lValue);
   char *buf=new char[lValue]; //注意建立緩衝區方法
   RegQueryValue(HKEY_LOCAL_MACHINE,"Software// MyItem//Name",buf,&lValue);
  
  LONG RegQueryValue(
   HKEY hKey, // handle to key to query
   LPCTSTR lpSubKey, // name of subkey to query
   LPTSTR lpValue, // buffer for returned string
   PLONG lpcbValue // receives size of returned string
  );
   If lpValue is NULL, and lpcbValue is non-NULL, the function returns ERROR_SUCCESS, and stores the size of the data, in bytes, in the variable pointed to by lpcbValue. This lets an application determine the best way to allocate a buffer for the value's data. 所以,我們要調用兩次RegQueryValue,第一次查詢鍵值長度,第二次獲得鍵值
  
  
  
  HKEY hkey;
   RegOpenKey(HKEY_LOCAL_MACHINE, "Software//MyItem//Name",&hkey);
   //打開主鍵
   DWORD dwType;
   DWORD dwAge;
   DWORD dwValue;
   RegQueryValueEx(hkey,"age",0,&dwType,(LPBYTE)&dwAge,&dwValue);
  
  其他函數:
  
   RegDeleteKey(); RegDeleteValue();等

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章