API讀寫INI文件(轉載)

 

API讀寫INI文件(轉載)

512人閱讀 評論(1) 收藏 舉報

用API寫INI文件的函數有
BOOL WritePrivateProfileString(

    LPCTSTR lpAppName, // 節名
    LPCTSTR lpKeyName, // 鍵名
    LPCTSTR lpString, // 添加的字符串
    LPCTSTR lpFileName  // Ini文件名
   );

BOOL WritePrivateProfileStruct(
    LPCTSTR lpszSection, // pointer to section name
    LPCTSTR lpszKey, // pointer to key name
    LPVOID lpStruct, // 要寫入的數據緩衝區
    UINT uSizeStruct, // 緩衝區的大小
    LPCTSTR szFile // pointer to initialization filename
   );
BOOL WritePrivateProfileSection(

    LPCTSTR lpAppName, // pointer to string with section name
    LPCTSTR lpString, // 寫入的字符串
    LPCTSTR lpFileName  // pointer to string with filename
   );
用API讀INI文件的函數有
DWORD GetPrivateProfileString(

    LPCTSTR lpAppName, // points to section name
    LPCTSTR lpKeyName, // points to key name
    LPCTSTR lpDefault, // 默認字符串 ,如果沒有則返回該值
    LPTSTR lpReturnedString, // 返回的字符串
    DWORD nSize, // 返回字符串的大小
    LPCTSTR lpFileName  // points to initialization filename
   );
DWORD GetPrivateProfileSection(

    LPCTSTR lpAppName, // address of section name
    LPTSTR lpReturnedString, // address of return buffer
    DWORD nSize, // size of return buffer
    LPCTSTR lpFileName  // address of initialization filename  
   );
UINT GetPrivateProfileInt(

    LPCTSTR lpAppName, // address of section name
    LPCTSTR lpKeyName, // address of key name
    INT nDefault, // return value if key name is not found
    LPCTSTR lpFileName  // address of initialization filename
   ); 
BOOL GetPrivateProfileStruct(

    LPCTSTR lpszSection, // address of section name
    LPCTSTR lpszKey, // address of key name
    LPVOID lpStruct, // address of return buffer
    UINT uSizeStruct, // size of return buffer
    LPCTSTR szFile // address of initialization filename
   );
DWORD GetPrivateProfileSectionNames(

    LPTSTR lpszReturnBuffer, // address of return buffer
    DWORD nSize, // size of return buffer
    LPCTSTR lpFileName // address of initialization filename
   );
當然還有如WriteProfileString,WriteProfileSection,WriteProfileSturct, GetProfileString,GetProfileStruct,GetProfileSectionNames,GetProfileInt,GetProfileSection但這些只對Win.ini有效
下面我們來學習它們的用法
WritePrivateProfileString函數是向ini文件中寫入字符串,如
WritePrivateProfileString(Pchar('類型'),Pchar('API'),Pchar('API 真好!'),Pchar('c:/example.ini'));
如果第二個參數是nil,那麼該操作將刪除該節
如果第三個參數爲nil,那麼該操作將刪除該節中的所有鍵
如果在指定的文件中沒有路徑,那麼它將在系統的目錄尋找文件,如果不存在則建立

WritePrivateProfileSection是向文件中寫入一整個鍵,其它鍵的格式爲key = value,如
WritePrivateProfileSection(Pchar('類型'),Pchar('其它=123'),Pchar('c:/example.ini'));
注意,該操作將刪除該節中的所有鍵後在進行本次的寫入

WritePrivateProfileStruct是向文件中寫入一個結構,如
type
  TPerson = record
    Name:string;
    Age:integer;
  end;
var
   Per:TPerson;
WritePrivateProfileStruct(Pchar('類型'),Pchar('結構'),@Per,Sizeof(Per),Pchar('C:/example.ini'));

GetPrivateProfileString是從文件中讀出一個指定鍵的字符串,如果沒有找到,則返回默認值
GetPrivateProfileString(Pchar(‘類型'),Pchar('API'),Pchar('no value'),Str1,21,Pchar('c:/example.ini'));

GetPrivateProfileSection是從文件中讀出一整個鍵
GetprivateProfileSection('類型',str1,200,'c:/example.ini');

GetPrivateProfileInt是從文件中讀出指定鍵的整型值,如果沒找到或不是整型的值,則返回默認值,如果返回值小於0,則返回0,如
i:=GetPrivateProfileInt(Pchar('類型'),Pchar('整型'),i,Pchar('C:/example.ini'));
showMessage(inttostr(i));

GetPrivateProfileStruct從文件中讀出指定鍵的結構值,如
type
  TPerson = record
    Name:string;
    Age:integer;
  end;
var
   Buffer:TPerson;
GetPrivateProfileStruct(Pchar('類型'),Pchar('結構'),@Buffer,Sizeof(Per),Pchar('C:/example.ini'));

GetPrivateProfileSectionNames是從文件中讀出所有節的名稱,該函數返回讀入緩衝區的字符數,如
count:=GetPrivateProfileSectionNames(Str3,200,Pchar('c:/example.ini'));
ShowMessage(Str3);
此處顯示的只是第一個節的名稱,如果要顯示第二個字符的名稱則要用到下面這句
showmessage(str3+5);
這句不用多解釋吧?
以上就是這些函數的用法。你可能要問“怎麼只有寫字符串的呀,怎麼沒有寫其它的類型的呢?”,問的好,不過其它類型的用WritePrivateProfileString都能代替,如要寫浮點型的就把該類型的放到’’當中,如’12.5’。那位學友又問了,“如果是讓用戶輸入,他們也不知道應該輸入什麼,怎麼能限制他們輸入的都是數字”,這方法可太多了,如用控件或檢查它們是不是在指定的數字,你可別說不會限制它們是不是數字呀*_^,如果真的不會,你就看它們的ASCII碼是不是在48-57之間就行了。“那讀出呢?”,Delphi不是有StrToInt,StrToFloat,StrToCurr等這麼函數嘛,可以用它們來轉換。
我在研究這些函數的同時,發現了一段有趣程序代碼,但我不知道它爲什麼要這樣做,有什麼好處,大家可以看看它們,不過是用C寫的,它們在Delphi SDK或MSDN中查找WritePrivateProfileString函數,在最下面的那個段代碼就是

如果我在上面說的有錯誤的話,希望大家指正,共同討論學習  

 

下面是另一網友文章

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

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

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