天天看點

配置檔案

getprivateprofile系列函數

1)寫入.ini檔案:

bool writeprivateprofilestring(

  lpctstr lpappname,  // ini檔案中的一個字段名[節名]可以有很多個節名

  lpctstr lpkeyname,  // lpappname 下的一個鍵名,也就是裡面具體的變量名

  lpctstr lpstring,   // 鍵值,也就是資料

  lpctstr lpfilename  // ini檔案的路徑

)

經典句:return writeprivateprofilestring(def_cloud_section, szkey,bvalue ? _t("1") : _t("0"), szinipath);

2)讀取.ini檔案:

    dword getprivateprofilestring(

      lpctstr lpappname,        // ini檔案中的一個字段名[節名]可以有很多個節名

      lpctstr lpkeyname,        // lpappname 下的一個鍵名,也就是裡面具體的變量名

      lpctstr lpdefault,        // 如果lpreturnedstring為空,則把個變量賦給lpreturnedstring

      lptstr lpreturnedstring,  // 存放鍵值的指針變量,用于接收ini檔案中鍵值(資料)的接收緩沖區

      dword nsize,            // lpreturnedstring的緩沖區大小

      lpctstr lpfilename        // ini檔案的路徑

    );

  <傳回值>  

   取得字元串 lpreturnedstring,  同時傳回一個整數,大小為取得字元串的長度。

3)讀取整形值:(傳回值為讀到的整)

    uint getprivateprofileint(

      lpctstr lpappname,  // ini檔案中的一個字段名[節名]可以有很多個節名

      lpctstr lpkeyname,  // lpappname 下的一個鍵名,也就是裡面具體的變量名

      int ndefault,       // 如果沒有找到指定的資料傳回,則把個變量值賦給傳回值

      lpctstr lpfilename  // ini檔案的路徑

int a= getprivateprofileint("b", "c", 1, "c:\\d.ini");

定義變量a 查找c盤d.ini配置檔案下的b小段目錄下的,c參數,如果找不到使用預設值1.

注:沒有writeprivateprofileint(),可以使用writeprivateprofilestring();

4)從指定的檔案中取得全部的關鍵字的值,擷取指定小節所有項名和值的一個清單

dword getprivateprofilesection(

  lpctstr lpappname,       //欲擷取的小節。注意這個字串不區分大小寫

  lptstr lpreturnedstring,   //項和值字串的清單。每個字串都由一個null字元分隔,最後一個字串後面用兩個null字元中止

  dword nsize,                 //緩沖區的大小。在windows系統中最大值為32767

  lpctstr lpfilename;        //初始化檔案的名字。如沒有指定完整路徑名,windows就在windows目錄中查找檔案

例子1:

  string strpath(config);

    strpath += "\\log.ini";

    cout<<strpath<<endl;

    int res = writeprivateprofilestringa("section", "key1", "123", strpath.c_str());//0表示失敗,非0表示成功

    cout<<res<<endl;

    int num = getprivateprofileinta("section", "key1", 100, strpath.c_str());//如果沒有section和key1的話才會傳回預設值100

    cout<<num<<endl;

    char str[10];

    num = getprivateprofilestringa("section2", "key3", "no find", str, sizeof(str), strpath.c_str());//會忽略value前的空格

    cout<<str<<endl;

    char str2[50];

    num = getprivateprofilesectiona("section", str2, sizeof(str2), strpath.c_str());

    for (char *p = str2; p < str2 + 50; p++)

    {

        cout<<*p;

    }

    cout<<endl;

例子2:

lptstr lppath = new char[max_path];

strcpy(lppath, "d:\\inifilename.ini");

writeprivateprofilestring("liming", "sex", "man", lppath);

writeprivateprofilestring("liming", "age", "20", lppath);

writeprivateprofilestring("fangfang", "sex", "woman", lppath);

writeprivateprofilestring("fangfang", "age", "21", lppath);

delete [] lppath;

ini檔案如下:

[liming]

sex=man

age=20

[fangfang]

sex=woman

age=21

讀ini檔案:

lptstr limingsex = new char[6];

int limingage;

lptstr fangfangsex = new char[6];

int fangfangage;

strcpy(lppath, "..\\inifilename.ini");

getprivateprofilestring("liming", "sex", "", limingsex, 6, lppath);

limingage = getprivateprofileint("liming", "age", 0, lppath);

getprivateprofilestring("fangfang", "sex", "", fangfangsex, 6, lppath);

fangfangage = getprivateprofileint("fangfang", "age", 0, lppath);

-------------------------------------------------------------------------

原文1:https://blog.csdn.net/wuguai4/article/details/7287346

參考2:https://www.cnblogs.com/bigcat814/archive/2012/12/17/2821364.html