最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C++讀寫(xiě)ini配置文件實(shí)現(xiàn)過(guò)程詳解

 更新時(shí)間:2020年07月29日 09:44:09   投稿:yaominghui  
這篇文章主要介紹了C++讀寫(xiě)ini配置文件實(shí)現(xiàn)過(guò)程詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

在Windows的VC下

讀ini文件

例如:在D:\test.ini文件中

[Font]
name=宋體
size= 12pt
color = RGB(255,0,0)

上面的=號(hào)兩邊可以加空格,也可以不加

用GetPrivateProfileInt()和GetPrivateProfileString()

[section]
key=string
   .
   .

獲取integer
UINT GetPrivateProfileInt(
 LPCTSTR lpAppName, // section name
 LPCTSTR lpKeyName, // key name
 INT nDefault,    // return value if key name not found
 LPCTSTR lpFileName // initialization file name
);

注意:lpAppName和lpKeyName不區(qū)分大小寫(xiě),當(dāng)獲得integer <0,那么返回0。lpFileName 必須是絕對(duì)路徑,因相對(duì)路徑是以C:\windows\

DWORD GetPrivateProfileString(
 LPCTSTR lpAppName,    // section name
 LPCTSTR lpKeyName,    // key name
 LPCTSTR lpDefault,    // default string
 LPTSTR lpReturnedString, // destination buffer
 DWORD nSize,       // size of destination buffer
 LPCTSTR lpFileName    // initialization file name
);

注意:lpAppName和lpKeyName不區(qū)分大小寫(xiě),若lpAppName為NULL,lpReturnedString緩沖區(qū)內(nèi)裝載這個(gè)ini文件所有小節(jié)的列表,若為lpKeyName=NULL,就在lpReturnedString緩沖區(qū)內(nèi)裝載指定小節(jié)所有項(xiàng)的列表。lpFileName 必須是絕對(duì)路徑,因相對(duì)路徑是以C:\windows\,
返回值:復(fù)制到lpReturnedString緩沖區(qū)的字符數(shù)量,其中不包括那些NULL中止字符。如lpReturnedString緩沖區(qū)不夠大,不能容下全部信息,就返回nSize-1(若lpAppName或lpKeyName為NULL,則返回nSize-2)

獲取某一字段的所有keys和values
DWORD GetPrivateProfileSection(
 LPCTSTR lpAppName,    // section name
 LPTSTR lpReturnedString, // return buffer
 DWORD nSize,       // size of return buffer
 LPCTSTR lpFileName    // initialization file name
);

retrieves the names of all sections in an initialization file.
DWORD GetPrivateProfileSectionNames(
 LPTSTR lpszReturnBuffer, // return buffer
 DWORD nSize,       // size of return buffer
 LPCTSTR lpFileName    // initialization file name
);
其實(shí)就等于,GetPrivateProfileString(NULL,NULL,lpszReturnedBuffer,nSize,lpFileName)

例子:

/* test.ini "="號(hào)兩邊可以加空格,也可以不加
  [Font]
  name=宋體
  size= 12pt
  color = RGB(255,0,0)
  [Layout]
  [Body]
  */

  CString strCfgPath = _T("D:\\test.ini"); //注意:'\\'
  LPCTSTR lpszSection = _T("Font");
  int n = GetPrivateProfileInt(_T("FONT"), _T("size"), 9, strCfgPath);//n=12
  CString str;
  GetPrivateProfileString(lpszSection, _T("size"), _T("9pt"), str.GetBuffer(MAX_PATH), MAX_PATH, strCfgPath);
  str.ReleaseBuffer();//str="12pt"

  TCHAR buf[200] = { 0 };
  int nSize = sizeof(buf) / sizeof(buf[0]);
  GetPrivateProfileString(lpszSection, NULL, _T(""), buf, nSize, strCfgPath);
  //buf: "name\0size\0color\0\0"

  memset(buf, 0, sizeof(buf));
  GetPrivateProfileString(NULL, _T("size"), _T(""), buf, nSize, strCfgPath);//沒(méi)Section,_T("size")沒(méi)意義了,所以可以寫(xiě)NULL
  //可以是 GetPrivateProfileString(NULL, NULL, _T(""), buf, nSize, strCfgPath);
  //buf: "Font\0Layout\0Body\0\0"

  memset(buf, 0, sizeof(buf));
  GetPrivateProfileSection(lpszSection, buf, nSize, strCfgPath);
  //buf: "name=宋體\0size=12pt\0color=RGB(255,0,0)\0\0"  此時(shí)“=”兩邊不會(huì)有空格

  memset(buf, 0, sizeof(buf));
  GetPrivateProfileSectionNames(buf, nSize, strCfgPath);//等于GetPrivateProfileString(NULL, NULL, _T(""), buf, nSize, strCfgPath);
  //buf: "Font\0Layout\0Body\0\0"

寫(xiě)ini文件

WritePrivateProfileString函數(shù),沒(méi)有寫(xiě)integer的,可以轉(zhuǎn)成string再寫(xiě)入。

BOOL WritePrivateProfileString(
 LPCTSTR lpAppName, // section name
 LPCTSTR lpKeyName, // key name
 LPCTSTR lpString,  // string to add
 LPCTSTR lpFileName // initialization file
);

The WritePrivateProfileSection function replaces the keys and values for the specified section in an initialization file. 

BOOL WritePrivateProfileSection(
 LPCTSTR lpAppName, // section name
 LPCTSTR lpString,  // data
 LPCTSTR lpFileName // file name
);

WritePrivateProfileString:

Remarks

If the lpFileName parameter does not contain a full path and file name for the file, WritePrivateProfileString searches the Windows directory for the file. If the file does not exist, this function creates the file in the Windows directory.

If lpFileName contains a full path and file name and the file does not exist, WritePrivateProfileString creates the file. The specified directory must already exist.

WritePrivateProfileSection:

Remarks

The data in the buffer pointed to by the lpString parameter consists of one or more null-terminated strings, followed by a final null character. Each string has the following form:

key=string

The WritePrivateProfileSection function is not case-sensitive; the string pointed to by the lpAppName parameter can be a combination of uppercase and lowercase letters.

If no section name matches the string pointed to by the lpAppName parameter, WritePrivateProfileSection creates the section at the end of the specified initialization file and initializes the new section with the specified key name and value pairs.

WritePrivateProfileSection deletes the existing keys and values for the named section and inserts the key names and values in the buffer pointed to by the lpString parameter. The function does not attempt to correlate old and new key names; if the new names appear in a different order from the old names, any comments associated with preexisting keys and values in the initialization file will probably be associated with incorrect keys and values.

This operation is atomic; no operations that read from or write to the specified initialization file are allowed while the information is being written.

例子:

WritePrivateProfileString(_T("Layout"), _T("left"), _T("100"), strCfgPath);
  WritePrivateProfileString(_T("Layout"), _T("top"), _T("80"), strCfgPath);
  //刪除某Section,包括[Layout]和其下所有Keys=Value
  WritePrivateProfileSection(_T("Layout"), NULL, strCfgPath);
  //刪除某Section,包括[Layout]下所有Keys=Value,但不刪除[Layout]
  WritePrivateProfileSection(_T("Layout"), _T(""), strCfgPath);
//而:WritePrivateProfileSection(NULL, NULL, strCfgPath);什么也不做,因Section為NULL

自己封裝的函數(shù):

獲取某一個(gè)Section的所有 key=value

map<CString, CString> GetKeysValues(LPCTSTR szSection, LPCTSTR szIniFilePath)

獲取ini文件的所有Section名

vector<CString> GetSectionsNames(LPCTSTR szIniFilePath)

#include <vector>
#include <map>
using std::vector;
using std::map;
//獲取ini文件的所有Section名
vector<CString> GetSectionsNames(LPCTSTR szIniFilePath)
{
  vector<CString> vRet;
  TCHAR buf[2048] = { 0 };
  long nSize = sizeof(buf) / sizeof(buf[0]);
  ::GetPrivateProfileSectionNames(buf, nSize, szIniFilePath);
  TCHAR *p, *q;
  p = q = buf;
  while (*p)//即 '\0' != *p
  {
    while (*q)
    {
      ++q;
    }
    CString str(p, q - p);
    vRet.push_back(str);
    p = q + 1;
    q = q + 1;
  }
  return vRet;
}
//獲取某一個(gè)Section的所有 key=value
map<CString, CString> GetKeysValues(LPCTSTR szSection, LPCTSTR szIniFilePath)
{
  map<CString,CString> mapRet;
  TCHAR buf[2048] = { 0 };
  long nSize = sizeof(buf) / sizeof(buf[0]);
  GetPrivateProfileSection(szSection, buf, nSize, szIniFilePath);
  TCHAR* p = buf;
  TCHAR* q = buf;
  while (*p)
  {
    CString strKey, strValue;
    while(*q)
    {
      if (_T('=') == *q)
      {
        strKey = CString(p, q - p);
        p = q + 1;
      }
      ++q;
    }
    strValue = CString(p, q - p);
    mapRet.insert(std::make_pair(strKey, strValue));
    p = q + 1;
    q = q + 1;
  }
  return mapRet;
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • C++線程池實(shí)現(xiàn)

    C++線程池實(shí)現(xiàn)

    線程池是一種并發(fā)編程技術(shù),通過(guò)預(yù)先創(chuàng)建一組線程并復(fù)用它們來(lái)執(zhí)行多個(gè)任務(wù),避免了頻繁創(chuàng)建和銷(xiāo)毀線程的開(kāi)銷(xiāo),本文就來(lái)介紹一下C++線程池實(shí)現(xiàn),感興趣的可以了解一下
    2025-02-02
  • C++中的類(lèi)型轉(zhuǎn)換static_cast、dynamic_cast、const_cast和reinterpret_cast總結(jié)

    C++中的類(lèi)型轉(zhuǎn)換static_cast、dynamic_cast、const_cast和reinterpret_cas

    這篇文章主要介紹了C++中的類(lèi)型轉(zhuǎn)換static_cast、dynamic_cast、const_cast和reinterpret_cast總結(jié),需要的朋友可以參考下
    2014-10-10
  • C語(yǔ)言判定一棵二叉樹(shù)是否為二叉搜索樹(shù)的方法分析

    C語(yǔ)言判定一棵二叉樹(shù)是否為二叉搜索樹(shù)的方法分析

    這篇文章主要介紹了C語(yǔ)言判定一棵二叉樹(shù)是否為二叉搜索樹(shù)的方法,結(jié)合實(shí)例形式綜合對(duì)比分析了C語(yǔ)言針對(duì)二叉搜索樹(shù)判定的原理、算法、效率及相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下
    2018-08-08
  • Sublime Text 3 實(shí)現(xiàn)C語(yǔ)言代碼的編譯和運(yùn)行(示例講解)

    Sublime Text 3 實(shí)現(xiàn)C語(yǔ)言代碼的編譯和運(yùn)行(示例講解)

    下面小編就為大家?guī)?lái)一篇Sublime Text 3 實(shí)現(xiàn)C語(yǔ)言代碼的編譯和運(yùn)行(示例講解)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-09-09
  • C++ abs函數(shù)實(shí)際應(yīng)用詳解

    C++ abs函數(shù)實(shí)際應(yīng)用詳解

    本文我們來(lái)講C++的abs函數(shù)以及實(shí)戰(zhàn)運(yùn)用,C++中的abs函數(shù)。在C++中使用abs函數(shù)要注意存在兩種版本,一種是在stdlmb.h中定義的版本,另一個(gè)是在cmath頭文件中定義的。夷實(shí)上在stdlib.h文件是C的函數(shù),而cmath中的是C++版本
    2022-08-08
  • C++類(lèi)中三大函數(shù)詳解(構(gòu)造、析構(gòu)和拷貝)

    C++類(lèi)中三大函數(shù)詳解(構(gòu)造、析構(gòu)和拷貝)

    c++三大函數(shù)指的是拷貝構(gòu)造、拷貝賦值、析構(gòu)函數(shù),下面這篇文章主要給大家介紹了關(guān)于C++類(lèi)中三大函數(shù)(構(gòu)造、析構(gòu)和拷貝)的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-03-03
  • 一文詳解C++11中decltype的使用

    一文詳解C++11中decltype的使用

    這篇文章主要為大家分享了C++11中decltype關(guān)鍵字的使用示例,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧
    2023-07-07
  • C++實(shí)現(xiàn)路口交通燈模擬系統(tǒng)

    C++實(shí)現(xiàn)路口交通燈模擬系統(tǒng)

    這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)路口交通燈模擬系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • C++用一棵紅黑樹(shù)同時(shí)封裝出set與map的實(shí)現(xiàn)代碼

    C++用一棵紅黑樹(shù)同時(shí)封裝出set與map的實(shí)現(xiàn)代碼

    set中存儲(chǔ)的一般為鍵K即可,而map存儲(chǔ)的一般都是鍵值對(duì)KV,也就是說(shuō)他們結(jié)構(gòu)是不同的,那么我們?nèi)绾尾拍苡靡活w紅黑樹(shù)同時(shí)封裝出set與map兩種容器呢,那么接下來(lái)我們具體地來(lái)研究下STL庫(kù)中是怎樣實(shí)現(xiàn)的,并且進(jìn)行模擬實(shí)現(xiàn),需要的朋友可以參考下
    2024-03-03
  • C++中函數(shù)匹配機(jī)制詳解

    C++中函數(shù)匹配機(jī)制詳解

    大家好,本篇文章主要講的是C++中函數(shù)匹配機(jī)制詳解,感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話(huà)記得收藏一下
    2022-02-02

最新評(píng)論

闵行区| 哈巴河县| 河曲县| 鄂托克前旗| 延边| 含山县| 拉萨市| 维西| 西丰县| 新邵县| 江北区| 旺苍县| 都昌县| 库车县| 长沙县| 常熟市| 潼关县| 沂源县| 门源| 九龙坡区| 临湘市| 玉屏| 万年县| 成安县| 宁明县| 历史| 界首市| 锦屏县| 城固县| 焉耆| 灌阳县| 白沙| 二手房| 屏东市| 宾阳县| 洛川县| 东乌珠穆沁旗| 杂多县| 武城县| 泰和县| 满洲里市|