VS INI檔案讀寫(僅供參考哦!)

來源:互聯網
上載者:User

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);
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.