VC中讀寫ini檔案

來源:互聯網
上載者:User

   在我們寫的程式當中,總有一些配置資訊需要儲存下來,以便完成程式的功能,最簡單的辦法就是將這些資訊寫入INI檔案中,程式初始化時再讀入.具體應用如下:
一.將資訊寫入.INI檔案中.
 1.所用的WINAPI函數原型為:
BOOL WritePrivateProfileString(
LPCTSTR lpAppName,
LPCTSTR lpKeyName,
LPCTSTR lpString,
LPCTSTR lpFileName
);
  其中各參數的意義:
   LPCTSTR lpAppName 是INI檔案中的一個欄位名.
   LPCTSTR lpKeyName 是lpAppName下的一個鍵名,通俗講就是變數名.
   LPCTSTR lpString 是索引值,也就是變數的值,不過必須為LPCTSTR型或CString型的.
   LPCTSTR lpFileName 是完整的INI檔案名稱.

 2.具體使用方法:設現有一名學生,需把他的姓名和年齡寫入 c:/stud/student.ini 檔案中.
  CString strName,strTemp;
  int nAge;
  strName="張三";
  nAge=12;
  ::WritePrivateProfileString("StudentInfo","Name",strName,"c://stud//student.ini");

  此時c:/stud/student.ini檔案中的內容如下:
 [StudentInfo]
      ame=張三
   3.要將學生的年齡儲存下來,只需將整型的值變為字元型即可:
  strTemp.Format("%d",nAge);
  ::WritePrivateProfileString("StudentInfo","Age",strTemp,"c://stud//student.ini");

二.將資訊從INI檔案中讀入程式中的變數.
 1.所用的WINAPI函數原型為:
DWORD GetPrivateProfileString(
LPCTSTR lpAppName,
LPCTSTR lpKeyName,
LPCTSTR lpDefault,
LPTSTR lpReturnedString,
DWORD nSize,
LPCTSTR lpFileName
);
  其中各參數的意義:
   前二個參數與 WritePrivateProfileString中的意義一樣.

   lpDefault : 如果INI檔案中沒有前兩個參數指定的欄位名或鍵名,則將此值賦給變數.
   lpReturnedString : 接收INI檔案中的值的CString對象,即目的緩衝器.
   nSize : 目的緩衝器的大小.
   lpFileName : 是完整的INI檔案名稱.

   2.具體使用方法:現要將上一步中寫入的學生的資訊讀入程式中.
  CString strStudName;
  int nStudAge;
  GetPrivateProfileString("StudentInfo","Name","預設姓名",strStudName.GetBuffer(MAX_PATH),MAX_PATH,"c://stud//student.ini");
    執行後 strStudName 的值為:"張三",若前兩個參數有誤,其值為:"預設姓名".

   3.讀入整型值要用另一個WINAPI函數:
UINT GetPrivateProfileInt(
LPCTSTR lpAppName,
LPCTSTR lpKeyName,
INT nDefault,
LPCTSTR lpFileName
);
  這裡的參數意義與上相同.使用方法如下:
 nStudAge=GetPrivateProfileInt("StudentInfo","Age",10,"c://stud//student.ini");

三.迴圈寫入多個值,設現有一程式,要將最近使用的幾個檔案名稱儲存下來,具體程式如下:
   1.寫入:
  CString strTemp,strTempA;
  int i;
  int nCount=6;
  file://共有6個檔案名稱需要儲存

  for(i=0;i {strTemp.Format("%d",i);
   strTempA=檔案名稱;
  file://檔案名稱可以從數組,列表框等處取得.
  ::WritePrivateProfileString("UseFileName","FileName"+strTemp,strTempA,"c://usefile//usefile.ini");
  }
  strTemp.Format("%d",nCount);
  ::WritePrivateProfileString("FileCount","Count",strTemp,"c://usefile//usefile.ini");
  file://將檔案總數寫入,以便讀出.

   2.讀出:
  nCount=::GetPrivateProfileInt("FileCount","Count",0,"c://usefile//usefile.ini");
  for(i=0;i
   {strTemp.Format("%d",i);

   strTemp="FileName"+strTemp;
   ::GetPrivateProfileString("CurrentIni",strTemp,"default.fil", strTempA.GetBuffer(MAX_PATH),MAX_PATH,"c://usefile//usefile.ini");

   file://使用strTempA中的內容.
  }

  補充四點:

   1.INI檔案的路徑必須完整,檔案名稱前面的各級目錄必須存在,否則寫入不成功,該函數返回 FALSE 值.

   2.檔案名稱的路徑中必須為 // ,因為在VC++中, // 才表示一個 / .

   3.也可將INI檔案放在程式所在目錄,此時 lpFileName 參數為: ".//student.ini".

   4.從網頁中粘貼原始碼時,最好先粘貼至記事本中,再往VC中粘貼,否則易造成編譯錯誤,開始時我也十分不解,好好的代碼怎麼就不對呢?後來才找到這個方法.還有一些代碼中使用了全形字元如:<,\等,也會造成編譯錯誤.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.