MFC文件讀寫之ini文件

什麼是ini文件?

initialization File,即爲初始化文件,是windows的系統配置文件所採用的存儲格式,統管windows的各項配置。或者作爲項目中的配置文件,爲整個項目所共用。

文件格式爲 節、鍵、值。其中節爲[Section name], 參數爲 key(鍵)=value(值)。

ini文件中註解使用分號表示(;),在分號後面的文字,直到該行結尾都全部爲註解。

[Section Name]用來表示一個段落,INI文件可能是項目中共用的,所以使用[Section Name]段名來區分不同用途的參數區。key就用來表示參數名,value表示對應的值。


ini文件讀


1、從系統的win.ini文件中讀取信息

(1) 讀取字符串

DWORD GetProfileString(

LPCTSTR lpAppName, // 節名【不區分大小寫】

LPCTSTR lpKeyName, // 鍵名,讀取該鍵的值【不區分大小寫

LPCTSTR lpDefault, // 若指定的鍵不存在,該值作爲讀取的默認值

LPTSTR lpReturnedString, // 一個指向緩衝區的指針,接收讀取的字符串

DWORD nSize // 指定lpReturnedString指向的緩衝區的大小

)


如:

CString str;

::GetProfileString("Test","id","Error",str.GetBuffer(20),20);


(2) 讀取整數

UINT GetProfileInt(

LPCTSTR lpAppName, // 同上

LPCTSTR lpKeyName, // 同上

INT nDefault // 若指定的鍵名不存在,該值作爲讀取的默認值

)

2 從自己的ini文件中讀取信息

函數原型

DWORD GetPrivateProfileString(string lpszSection,string lpszKey,string lpszDefault, lpszReturnBuffer, cchReturnBuffer, lpszFile)

GetPrivateProfileString("DBInfo","IP","",IP.GetBuffer(MAX_PATH),MAX_PATH,iniFilePath);【測過】

CString IP, iniFilePath.

GetPrivateProfileString(Section name,key,"",key.GetBuffer(MAX_PATH),MAX_PATH,ini文件路徑+文件名);

其中第一個參數爲段名,第二個參數爲參數名稱,第三個參數爲沒找到key返回的默認值,第四個參數爲指定一個字串緩衝區,第五個參數爲指定裝載到lpReturnedString緩衝區的最大字符數量,第六個參數爲初始化文件的名字,如沒有指定一個完整路徑名,windows就在Windows目錄中查找文件。


__inline DWORD GetPrivateProfileString(

LPCTSTR lpAppName,

LPCTSTR lpKeyName,

LPCTSTR lpDefault,

LPTSTR lpReturnedString,

DWORD nSize,

LPCTSTR lpFileName

)



DWORDGetPrivateProfileInt (string lpszSection,string lpszKey,long lpszDefault, lpszFile) 爲初始化文件中指定的條目獲取一個整數值,參數列表跟上文取String一致,而第三個參數是如果找不到則返回一個默認的long類型的值。

特別注意的是 如果找到的數字不是一個合法的整數,函數會返回其中合法的一部分。比如“xyz=55zz”這個條目,函數返回55。

UINT GetPrivateProfileInt(
LPCTSTR lpAppName,
LPCTSTR lpKeyName,
INT nDefault,
LPCTSTR lpFileName
);



寫INI文件


1. 把信息寫入系統的win.ini文件

BOOL WriteProfileString(

LPCTSTR lpAppName, // 節的名字,是一個以0結束的字符串

LPCTSTR lpKeyName, // 鍵的名字,是一個以0結束的字符串。若爲NULL,則刪除整個節

LPCTSTR lpString // 鍵的值,是一個以0結束的字符串。若爲NULL,則刪除對應的鍵

)


2. 把信息寫入自己定義的.ini文件

BOOL WritePrivateProfileString(

LPCTSTR lpAppName, // 同上

LPCTSTR lpKeyName, // 同上

LPCTSTR lpString, // 同上

LPCTSTR lpFileName // 要寫入的文件的文件名。若該ini文件與程序在同一個目錄下,也可使用相對

//路徑,否則需要給出絕度路徑。

)

如:

::WriteProfileString("Test","id","xym");

//在win.ini中創建一個Test節,並在該節中創建一個鍵id,其值爲xym

::WritePrivateProfileString("Test","id","xym","d:\\vc\\Ex1\\ex1.ini");

//在Ex1目錄下的ex1.ini中創建一個Test節,並在該節中創建一個鍵id,其值爲xym

//若Ex1.ini文件與讀寫該文件的程序在同一個目錄下,則上面語句也可寫爲:

::WritePrivateProfileString("Test","id","xym",".\\ex1.ini");

需要注意的是,C系列的語言中,轉義字符'\\'表示反斜線'\'。另外,當使用相對路徑時,\\前的.號不能丟掉了。





在我們的程序設計中經常需要對一些參數進行配置,配置好後還要在下一次啓動仍然有效,那麼一個有效的可行辦法就是使用ini文件,也就是Windows初始化文件來保存一些我們的設置,然後讓程序啓動的時候從這個ini文件中讀取相關配置。我們需要做以下的工作。

     1.創建此ini文件,Windows對ini文件的操作有專門的函數,我們經常用的就是WritePrivateProfileString()和GetPrivateProfileString()了。那麼我們在程序的初始化時首先檢查是否存在ini文件,如果不存在則創建一個默認的ini文件。

         我們使用CFileFind類查找我們需要的ini文件是否存在,若不存在則創建一個。WritePrivateProfileString()當ini不存在時會自動創建一個ini文件在指定路徑。

  1. //向D盤寫入程序數據庫連接ini文件信息,默認設置如下

  2. CFileFind finder;   //查找是否存在ini文件,若不存在,則生成一個新的默認設置的ini文件,這樣就保證了我們更改後的設置每次都可用

  3. BOOL ifFind = finder.FindFile(_T("d:\\RoadDataManagerApp.ini"));  

  4. if( !ifFind )  

  5. {  

  6.    ::WritePrivateProfileStringW(_T("Database connection Info"),_T("IP"),_T("10.210.0.9"),_T("d:\\RoadDataManagerApp.ini"));  

  7.    ::WritePrivateProfileStringW(_T("Database connection Info"),_T("Database"),_T("RoadNetData"),_T("d:\\RoadDataManagerApp.ini"));  

  8.    ::WritePrivateProfileStringW(_T("Database connection Info"),_T("UID"),_T("sa"),_T("d:\\RoadDataManagerApp.ini"));  

  9.    ::WritePrivateProfileStringW(_T("Database connection Info"),_T("PWD"),_T("4814278"),_T("d:\\RoadDataManagerApp.ini"));  

  10. }  



      2.在剛纔創建的ini文件中賦值,也就是WritePrivateProfileString()中所做的。

      3.在需要用到ini文件的地方從ini文件中讀取數據。例如:


  1.       ::GetPrivateProfileStringW(_T("Database connection Info"),_T("IP"),_T("沒找到IP信息"),m_strCurrentIP.GetBuffer(MAX_PATH),MAX_PATH,_T("d:\\RoadDataManagerApp.ini"));  

  2. ::GetPrivateProfileStringW(_T("Database connection Info"),_T("Database"),_T("沒找到數據庫名信息"),m_strCurrentDBName.GetBuffer(MAX_PATH),MAX_PATH,_T("d:\\RoadDataManagerApp.ini"));  

  3. ::GetPrivateProfileStringW(_T("Database connection Info"),_T("UID"),_T("沒找到用戶名信息"),m_strCurrentUID.GetBuffer(MAX_PATH),MAX_PATH,_T("d:\\RoadDataManagerApp.ini"));  

  4. ::GetPrivateProfileStringW(_T("Database connection Info"),_T("PWD"),_T("沒找到用戶密碼信息"),m_strCurrentPWD.GetBuffer(MAX_PATH),MAX_PATH,_T("d:\\RoadDataManagerApp.ini"));  


  5. //在這裏必須ReleaseBuffer(),否則無法再後面進行字符串的連接

  6. m_strCurrentIP.ReleaseBuffer();  

  7. m_strCurrentDBName.ReleaseBuffer();  

  8. m_strCurrentUID.ReleaseBuffer();  

  9. m_strCurrentPWD.ReleaseBuffer();  



          這樣基本的操作我們就都知道了。下面詳細說明對ini文件的各種操作。


INI文件簡介

在我們寫程序時,總有一些配置信息需要保存下來,以便在下一次啓動程序完成初始化,這實際上是一種類持久化。將一些信息寫入INI文件(initialization file)中,可完成簡單的持久化支持。

Windows提供了API接口用於操作INI文件,其支持的INI文件格式一般如下:

===============================

[Section1]

Key11=value11

Key12=value12

[Section2]

Key21=value21

Key22=value22

...

[SectionN]

KeyN1=valueN1

KeyN2=valueN2

===============================

一般一個INI文件可有N個節,每節可有n個鍵名及值對應,每個鍵名及其值以等式形式佔一行。

一般鍵的名稱可任取,不過建議用有意義的字符及詞構成。值一般可爲整數和字符串,其它類型要進行轉換。

常見的系統配置文件:

C:/boot.ini

C:/WINDOWS/win.ini

C:/WINDOWS/system.ini

C:/WINDOWS/desktop.ini

C:/WINDOWS/Resources/Themes/Windows Classic.theme

注意,字符串存貯在INI文件中時沒有引號;key和value之間的等號前後不容空格;註釋以分號“;”開頭。


VC中操作INI文件的API

(1)操作系統配置文件Win.ini的函數:


函數名

功能

GetProfileSection

讀取win.ini中指定節lpAppName中所有鍵名及其值。lpReturnedString字符串形式如下:

Key1=Value1/0Key2=Value2/0…KeyN=ValueN/0/0

GetProfileString

讀取win.ini中指定節lpAppName中鍵名爲lpKeyName對應變量的字符串值。

GetProfileInt

讀取win.ini中指定節lpAppName中鍵名爲lpKeyName對應變量的整數值。



WriteProfileSection

寫(替換)win.ini中指定節lpAppName中的鍵值。

lpString字符串形式同GetProfileSection中的lpReturnedString

WriteProfileString

寫(替換)win.ini中指定節lpAppName中鍵名爲lpKeyName對應變量的字符串值。


(2)操作用戶自定義配置文件(PrivateProfile.ini)的函數:


函數名

功能

GetPrivateProfileSectionNames

讀取lpFileName指定的配置文件中所有的節名。lpszReturnBuffer字符串形式如下:

Section1/0Section2/0…SectionN/0/0

GetPrivateProfileSection

GetProfileSection。

GetPrivateProfileString

GetProfileString。

GetPrivateProfileInt    

GetProfileInt

GetPrivateProfileStruct

須同WritePrivateProfileStruct配套使用。



WritePrivateProfileSection

WriteProfileSection

WritePrivateProfileString

WriteProfileString

WritePrivateProfileStruct

不常用。


注意:

(1)使用得最頻繁的是 GetPrivateProfileString 和 WritePrivateProfileString,沒有WriteProfileInt/WritePrivateProfileInt函數。

(2)Get系列讀取節鍵值,如果文件路徑有誤或節鍵名不對則返回設定的默認值。

(3)訪存自定義配置文件時,文件路徑lpFileName必須完整,文件名前面的各級目錄必須存在。如果lpFileName文件路徑不存在,則函數返回FALSE,GetLastError() = ERROR_PATH_NOT_FOUND。如果路徑正確,但是文件不存在,則該函數將先創建該文件。如果路徑及文件存在,則在現有ini文件基礎上進行讀寫。

如果 lpFileName 只指定文件名而沒有路徑的話,調用API將會去 Windows 的安裝目錄去查找而不會在當前目錄查找。

(4)要對調用API的模塊(exe)所在目錄下進行配置文件操作,可使用形如“.//config.ini”的相對路徑,注意轉義符。

(5)調用WritePrivateProfileSection,若參數三 lpString爲NULL,則可將對應section的全部內容清空;調用WritePrivateProfileString,若參數三 lpString爲NULL,則可將對應key刪除。


跨平臺配置文件

INI文件本質是對文件和字符串的處理,因此在跨平臺項目中的配置文件可以基於<stdio.h>中的標C文件FILE,然後實現像類似以上對節([Section])、鍵(Key)和值(Value)的字符串讀寫功能。

鑑於XML的樹形描述層次結構性清晰,現在很多軟件都大面積使用XML文件進行配置,如QQ的全局配置文件C:/Program Files/Tencent/QQ/gf-config.xml。java程序的配置文件基本都使用XML格式,C++中並沒有操作XML文件的標準庫。

在C/C++程序中要使用XML做爲配置文件,涉及到XML的解析。Windows平臺可使用MsXml對XML進行解析,參考《MsXml創建和解析XML示例》,跨平臺可以考慮自己實現,或使用C++ BOOST正則表達式,或選擇Free C or C++ XML Parser Libraries,如XmlParserTinyXMLCMarkuplibxml等。


CIniFile類

以下提供對Windows操作INI文件的API的簡單封裝類CIniFile。


  1. // IniFile.h  

  2. #ifndef __INIFILE_H__  

  3. #define __INIFILE_H__  

  4. class CIniFile    

  5. {    

  6. public:    

  7.    CIniFile();    

  8.    CIniFile(LPCTSTR szFileName);    

  9. virtual ~CIniFile();    

  10. public:    

  11. // Attributes      

  12. void SetFileName(LPCTSTR szFileName);    

  13. public:    

  14. // Operations  

  15. BOOL SetProfileInt(LPCTSTR lpszSectionName, LPCTSTR lpszKeyName, int nKeyValue);    

  16. BOOL SetProfileString(LPCTSTR lpszSectionName, LPCTSTR lpszKeyName, LPCTSTR lpszKeyValue);    

  17. DWORD GetProfileSectionNames(CStringArray& strArray); // 返回section數量  

  18. int GetProfileInt(LPCTSTR lpszSectionName, LPCTSTR lpszKeyName);    

  19. DWORD GetProfileString(LPCTSTR lpszSectionName, LPCTSTR lpszKeyName, CString& szKeyValue);    

  20. BOOL DeleteSection(LPCTSTR lpszSectionName);    

  21. BOOL DeleteKey(LPCTSTR lpszSectionName, LPCTSTR lpszKeyName);    

  22. private:    

  23.    CString  m_szFileName; // .//Config.ini, 如果該文件不存在,則exe第一次試圖Write時將創建該文件  

  24. UINT m_unMaxSection; // 最多支持的section數(256)  

  25. UINT m_unSectionNameMaxSize; // section名稱長度,這裏設爲32(Null-terminated)  

  26. void Init();    

  27. };    

  28. #endif  

  29. // IniFile.cpp  

  30. #include "IniFile.h"  

  31. void CIniFile::Init()    

  32. {    

  33.    m_unMaxSection = 512;    

  34.    m_unSectionNameMaxSize = 33; // 32位UID串  

  35. }    

  36. CIniFile::CIniFile()    

  37. {    

  38.    Init();    

  39. }    

  40. CIniFile::CIniFile(LPCTSTR szFileName)    

  41. {    

  42. // (1) 絕對路徑,需檢驗路徑是否存在  

  43. // (2) 以"./"開頭,則需檢驗後續路徑是否存在  

  44. // (3) 以"../"開頭,則涉及相對路徑的解析  

  45.    Init();    

  46. // 相對路徑  

  47.    m_szFileName.Format(".//%s", szFileName);    

  48. }    

  49. CIniFile::~CIniFile()      

  50. {    

  51. }    

  52. void CIniFile::SetFileName(LPCTSTR szFileName)    

  53. {    

  54.    m_szFileName.Format(".//%s", szFileName);    

  55. }    

  56. DWORD CIniFile::GetProfileSectionNames(CStringArray &strArray)    

  57. {    

  58. int nAllSectionNamesMaxSize = m_unMaxSection*m_unSectionNameMaxSize+1;    

  59. char *pszSectionNames = newchar[nAllSectionNamesMaxSize];    

  60. DWORD dwCopied = 0;    

  61.    dwCopied = ::GetPrivateProfileSectionNames(pszSectionNames, nAllSectionNamesMaxSize, m_szFileName);    

  62.    strArray.RemoveAll();    

  63. char *pSection = pszSectionNames;    

  64. do

  65.    {    

  66.        CString szSection(pSection);    

  67. if (szSection.GetLength() < 1)    

  68.        {    

  69. delete[] pszSectionNames;    

  70. return dwCopied;    

  71.        }    

  72.        strArray.Add(szSection);    

  73.        pSection = pSection + szSection.GetLength() + 1; // next section name  

  74.    } while (pSection && pSection<pszSectionNames+nAllSectionNamesMaxSize);    

  75. delete[] pszSectionNames;    

  76. return dwCopied;    

  77. }    

  78. DWORD CIniFile::GetProfileString(LPCTSTR lpszSectionName, LPCTSTR lpszKeyName, CString& szKeyValue)    

  79. {    

  80. DWORD dwCopied = 0;    

  81.    dwCopied = ::GetPrivateProfileString(lpszSectionName, lpszKeyName, "",    

  82.        szKeyValue.GetBuffer(MAX_PATH), MAX_PATH, m_szFileName);    

  83.    szKeyValue.ReleaseBuffer();    

  84. return dwCopied;    

  85. }    

  86. int CIniFile::GetProfileInt(LPCTSTR lpszSectionName, LPCTSTR lpszKeyName)    

  87. {    

  88. int nKeyValue = ::GetPrivateProfileInt(lpszSectionName, lpszKeyName, 0, m_szFileName);    

  89. return nKeyValue;    

  90. }    

  91. BOOL CIniFile::SetProfileString(LPCTSTR lpszSectionName, LPCTSTR lpszKeyName, LPCTSTR lpszKeyValue)    

  92. {    

  93. return ::WritePrivateProfileString(lpszSectionName, lpszKeyName, lpszKeyValue, m_szFileName);    

  94. }    

  95. BOOL CIniFile::SetProfileInt(LPCTSTR lpszSectionName, LPCTSTR lpszKeyName, int nKeyValue)    

  96. {    

  97.    CString szKeyValue;    

  98.    szKeyValue.Format("%d", nKeyValue);    

  99. return ::WritePrivateProfileString(lpszSectionName, lpszKeyName, szKeyValue, m_szFileName);    

  100. }    

  101. BOOL CIniFile::DeleteSection(LPCTSTR lpszSectionName)    

  102. {    

  103. return ::WritePrivateProfileSection(lpszSectionName, NULL, m_szFileName);    

  104. }    

  105. BOOL CIniFile::DeleteKey(LPCTSTR lpszSectionName, LPCTSTR lpszKeyName)    

  106. {    

  107. return ::WritePrivateProfileString(lpszSectionName, lpszKeyName, NULL, m_szFileName);    

  108. }    


在VC++中讀寫INI文件
  在我們寫的程序當中,總有一些配置信息需要保存下來,以便完成程序的功能,最簡單的辦法就是將這些信息寫入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]
   Name=張三

  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中粘貼,否則易造成編譯錯誤,開始時我也十分不解,好好的代碼怎麼就不對呢?後來才找到這個方法.還有一些代碼中使用了全角字符如:<,\等,也會
造成編譯錯誤.






============


VC中用函數讀寫ini文件的方法

        ini文件(即Initialization file),這種類型的文件中通常存放的是一個程序的初始化信息。ini文件由若干個節(Section)組成,每個Section由若干鍵(Key)組成,每個Key可以賦相應的值。讀寫ini文件實際上就是讀寫某個的Section中相應的Key的值,而這隻要藉助幾個函數即可完成。


一、向ini文件中寫入信息的函數
1. 把信息寫入系統的win.ini文件
BOOL WriteProfileString(
     LPCTSTR lpAppName, // 節的名字,是一個以0結束的字符串
     LPCTSTR lpKeyName, // 鍵的名字,是一個以0結束的字符串。若爲NULL,則刪除整個節
     LPCTSTR lpString       // 鍵的值,是一個以0結束的字符串。若爲NULL,則刪除對應的鍵
)


2. 把信息寫入自己定義的.ini文件
BOOL WritePrivateProfileString(
     LPCTSTR lpAppName,      // 同上
     LPCTSTR lpKeyName,      // 同上
     LPCTSTR lpString,       // 同上
     LPCTSTR lpFileName      // 要寫入的文件的文件名。若該ini文件與程序在同一個目錄下,也可使用相對
           //路徑,否則需要給出絕度路徑。
)

如:
::WriteProfileString("Test","id","xym");
//在win.ini中創建一個Test節,並在該節中創建一個鍵id,其值爲xym

::WritePrivateProfileString("Test","id","xym","d://vc//Ex1//ex1.ini");
//在Ex1目錄下的ex1.ini中創建一個Test節,並在該節中創建一個鍵id,其值爲xym

//若Ex1.ini文件與讀寫該文件的程序在同一個目錄下,則上面語句也可寫爲:
::WritePrivateProfileString("Test","id","xym",".//ex1.ini");

需要注意的是,C系列的語言中,轉義字符'//'表示反斜線'/'。另外,當使用相對路徑時,//前的.號不能丟掉了。

二、從ini文件中讀取數據的函數
1、從系統的win.ini文件中讀取信息
(1) 讀取字符串
DWORD GetProfileString(
     LPCTSTR lpAppName,            // 節名
     LPCTSTR lpKeyName,            // 鍵名,讀取該鍵的值
     LPCTSTR lpDefault,            // 若指定的鍵不存在,該值作爲讀取的默認值
     LPTSTR lpReturnedString,      // 一個指向緩衝區的指針,接收讀取的字符串
     DWORD nSize                   // 指定lpReturnedString指向的緩衝區的大小
)

如:
CString str;
::GetProfileString("Test","id","Error",str.GetBuffer(20),20);

(2) 讀取整數
UINT GetProfileInt(
     LPCTSTR lpAppName,      // 同上
     LPCTSTR lpKeyName,      // 同上
     INT nDefault            // 若指定的鍵名不存在,該值作爲讀取的默認值
)

如使用以下語句寫入了年齡信息:
::WriteProfileString("Test","age","25");
//在win.ini中創建一個Test節,並在該節中創建一個鍵age,其值爲25

則可用以下語句讀取age鍵的值:
int age;
age=::GetProfileInt("Test","age",0);

2、從自己的ini文件中讀取信息
(1) 讀取字符串
DWORD GetPrivateProfileString(
     LPCTSTR lpAppName,            // 同1(1)
     LPCTSTR lpKeyName,            // 同1(1)
     LPCTSTR lpDefault,            // 同1(1)
     LPTSTR lpReturnedString,      // 同1(1)
     DWORD nSize,                  // 同1(1)
     LPCTSTR lpFileName            // 讀取信息的文件名。若該ini文件與程序在同一個目錄下,也可使用相      
           //對路徑,否則需要給出絕度路徑。
)

如:
CString str;
::GetPrivateProfileString("Test","id","Error",str.GetBuffer(20),20,".//ex1.ini");
或:
::GetPrivateProfileString("Test","id","Error",str.GetBuffer(20),20,"d://vc//Ex1//ex1.ini");

(2) 讀取整數

UINT GetPrivateProfileInt(
     LPCTSTR lpAppName,      // 同上
     LPCTSTR lpKeyName,      // 同上
     INT nDefault,           // 若指定的鍵名不存在,該值作爲讀取的默認值
     LPCTSTR lpFileName      // 同上
)

如使用以下語句寫入了年齡信息:
::WritePrivateProfileString("Test","age","25",".//ex1.ini");
//在ex1.ini中創建一個Test節,並在該節中創建一個鍵age,其值爲25

則可用以下語句讀取age鍵的值:
int age;
age=::GetPrivateProfileInt("Test","age",0,".//ex1.ini");

三、 刪除鍵值或節

      回顧一下WriteProfileString函數的說明
BOOL WriteProfileString(
     LPCTSTR lpAppName, // 節的名字,是一個以0結束的字符串
     LPCTSTR lpKeyName, // 鍵的名字,是一個以0結束的字符串。若爲NULL,則刪除整個節
     LPCTSTR lpString       // 鍵的值,是一個以0結束的字符串。若爲NULL,則刪除對應的鍵
)

      由此可見,要刪除某個節,只需要將WriteProfileString第二個參數設爲NULL即可。而要刪除某個鍵,則只需要將該函數的第三個參數設爲 NULL即可。這是刪除系統的win.ini中的節或鍵,類似的,要刪除自己定義的ini文件中的節或鍵,也可做相同的操作。
      如:
::WriteProfileString("Test",NULL,NULL);      //刪除win.ini中的Test節
::WriteProfileString("Test","id",NULL);      //刪除win.ini中的id鍵

::WritePrivateProfileString("Test",NULL,NULL,".//ex1.ini");      //刪除ex1.ini中的Test節
::WritePrivateProfileString("Test","id",NULL,".//ex1.ini");      //刪除ex1.ini中的id鍵

四、如何判斷一個ini文件中有多少個節
      要判斷一個ini文件中有多少個節,最簡單的辦法就是將所有的節名都找出來,然後統計節名的個數。而要將所有的節名找出來,使用GetPrivateProfileSectionNames函數就可以了,其原型如下:
DWORD GetPrivateProfileSectionNames(
     LPTSTR lpszReturnBuffer,      // 指向一個緩衝區,用來保存返回的所有節名
     DWORD nSize,                  // 參數lpszReturnBuffer的大小
     LPCTSTR lpFileName            // 文件名,若該ini文件與程序在同一個目錄下,

                                               //也可使用相對路徑,否則需要給出絕度路徑
)

下面的是用來統計一個ini文件中共有多少個節的函數,當然,如果需要同時找到每個節中的各個鍵及其值,根據找到節名就可以很容易的得到了。


/*統計共有多少個節
節名的分離方法:若chSectionNames數組的第一字符是'/0'字符,則表明
有0個節。否則,從chSectionNames數組的第一個字符開始,順序往後找,
直到找到一個'/0'字符,若該字符的後繼字符不是 '/0'字符,則表明前
面的字符組成一個節名。若連續找到兩個'/0'字符,則統計結束*/


int CTestDlg::CalcCount(void)
{
TCHAR       chSectionNames[2048]={0};       //所有節名組成的字符數組
char * pSectionName; //保存找到的某個節名字符串的首地址
int i;       //i指向數組chSectionNames的某個位置,從0開始,順序後移
int j=0;      //j用來保存下一個節名字符串的首地址相對於當前i的位置偏移量
int count=0;      //統計節的個數

//CString name;
//char id[20];
::GetPrivateProfileSectionNames(chSectionNames,2048,".//ex1.ini");  
for(i=0;i<2048;i++,j++)
{
     if(chSectionNames[0]=='/0')
      break;       //如果第一個字符就是0,則說明ini中一個節也沒有
     if(chSectionNames[i]=='/0')
     {
      pSectionName=&chSectionNames[i-j]; //找到一個0,則說明從這個字符往前,減掉j個偏移量,
           //就是一個節名的首地址

      j=-1;         //找到一個節名後,j的值要還原,以統計下一個節名地址的偏移量
           //賦成-1是因爲節名字符串的最後一個字符0是終止符,不能作爲節名

           //的一部分
      /*::GetPrivateProfileString(pSectionName,"id","Error",id,20,".//ex1.ini");
      name.Format("%s",id);*/  
      //在獲取節名的時候可以獲取該節中鍵的值,前提是我們知道該節中有哪些鍵。

      AfxMessageBox(pSectionName);      //把找到的顯示出來

      if(chSectionNames[i+1]==0)
      {
        break;      //當兩個相鄰的字符都是0時,則所有的節名都已找到,循環終止
      }
     }  
}
return count;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////

在VC程序中利用系統提供的GetPrivateProfileString及WritePrivateProfileString函數直接讀寫系統配置ini文件(指定目錄下的Ini文件)

假設在當前目錄下有一個文件名爲Tets.ini的文件
用於保存用戶名和密碼
文件格式如下:
[Section1]
Item1=huzhifeng
Item2=1234565

1.寫INI文件
void CINI_File_TestDlg::OnButtonWrite()
{
// TODO: Add your control notification handler code here

CString strSection       = "Section1";
 CString strSectionKey    = "Item1";
char strBuff[256];
 CString strValue       = _T("");
CString strFilePath;

strFilePath=GetCurrentDirectory(256,strBuff);  //獲取當前路徑
strFilePath.Format("%s//Test.ini",strBuff);

GetDlgItemText(IDC_EDIT_NAME,strValue);     //獲取文本框內容:即姓名
WritePrivateProfileString(strSection,strSectionKey,strValue,strFilePath);  //寫入ini文件中相應字段

strSectionKey="Item2";
GetDlgItemText(IDC_EDIT_PASSWORD,strValue);   //獲取文本框內容:即密碼
WritePrivateProfileString(strSection,strSectionKey,strValue,strFilePath);
}

2.讀INI文件內容
void CINI_File_TestDlg::OnButtonRead()
{
// TODO: Add your control notification handler code here
CString strSection       = "Section1";
 CString strSectionKey    = "Item1";
char strBuff[256];
CString strValue       = _T("");
CString strFilePath;

strFilePath=GetCurrentDirectory(256,strBuff);  //獲取當前路徑
strFilePath.Format("%s//Test.ini",strBuff);

GetPrivateProfileString(strSection,strSectionKey,NULL,strBuff,80,strFilePath); //讀取ini文件中相應字段的內容
strValue=strBuff;
SetDlgItemText(IDC_EDIT_NAME,strValue);

strSectionKey="Item2";
GetPrivateProfileString(strSection,strSectionKey,NULL,strBuff,80,strFilePath);
strValue=strBuff;
SetDlgItemText(IDC_EDIT_PASSWORD,strValue);

UpdateData(FALSE);
}


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