欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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)文章

  • 利用Matlab繪制甘特圖的方法詳解

    利用Matlab繪制甘特圖的方法詳解

    這篇文章主要為大家詳細(xì)介紹了如何利用Matlab實(shí)現(xiàn)甘特圖(gantt?chart)的繪制,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)Matlab有一定幫助,需要的可以參考一下
    2022-10-10
  • C語(yǔ)言函數(shù)的遞歸和調(diào)用實(shí)例分析

    C語(yǔ)言函數(shù)的遞歸和調(diào)用實(shí)例分析

    一個(gè)函數(shù)在它的函數(shù)體內(nèi)調(diào)用它自身稱為遞歸調(diào)用。這種函數(shù)稱為遞歸函數(shù)。C語(yǔ)言允許函數(shù)的遞歸調(diào)用。在遞歸調(diào)用中,主調(diào)函數(shù)又是被調(diào)函數(shù)。執(zhí)行遞歸函數(shù)將反復(fù)調(diào)用其自身,每調(diào)用一次就進(jìn)入新的一層
    2013-07-07
  • 算法詳解之回溯法具體實(shí)現(xiàn)

    算法詳解之回溯法具體實(shí)現(xiàn)

    這篇文章主要介紹了算法詳解之回溯法具體實(shí)現(xiàn),需要的朋友可以參考下
    2014-02-02
  • 詳解C語(yǔ)言中的動(dòng)態(tài)內(nèi)存管理

    詳解C語(yǔ)言中的動(dòng)態(tài)內(nèi)存管理

    對(duì)于數(shù)據(jù)的存儲(chǔ)我們可以靜態(tài)存儲(chǔ),也可以動(dòng)態(tài)存儲(chǔ),兩種方式都有自己特有的好處,這篇文章教我們?nèi)绾瓦M(jìn)行動(dòng)態(tài)的數(shù)據(jù)存儲(chǔ)!?。。「信d趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2022-12-12
  • C語(yǔ)言攝氏度互相轉(zhuǎn)換華氏

    C語(yǔ)言攝氏度互相轉(zhuǎn)換華氏

    這篇文章主要介紹了C語(yǔ)言攝氏度互相轉(zhuǎn)換華氏,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-01-01
  • C++入門(mén)(命名空間,缺省參數(shù),函數(shù)重載,引用,內(nèi)聯(lián)函數(shù),auto,范圍for)

    C++入門(mén)(命名空間,缺省參數(shù),函數(shù)重載,引用,內(nèi)聯(lián)函數(shù),auto,范圍for)

    這篇文章主要介紹了C++入門(mén)(命名空間,缺省參數(shù),函數(shù)重載,引用,內(nèi)聯(lián)函數(shù),auto,范圍for),這些基礎(chǔ)知識(shí)是學(xué)習(xí)C++最最基礎(chǔ)需要掌握的知識(shí)點(diǎn),需要的朋友可以參考下
    2021-05-05
  • C++ DLL動(dòng)態(tài)庫(kù)的創(chuàng)建與調(diào)用(類庫(kù),隱式調(diào)用)

    C++ DLL動(dòng)態(tài)庫(kù)的創(chuàng)建與調(diào)用(類庫(kù),隱式調(diào)用)

    本文主要介紹了C++ DLL動(dòng)態(tài)庫(kù)的創(chuàng)建與調(diào)用(類庫(kù),隱式調(diào)用),文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-05-05
  • 一波C語(yǔ)言字符數(shù)組實(shí)用技巧集錦

    一波C語(yǔ)言字符數(shù)組實(shí)用技巧集錦

    這篇文章主要介紹了一波C語(yǔ)言字符數(shù)組實(shí)用技巧集錦,包括許多字符的轉(zhuǎn)換與提取等基本操作示例,需要的朋友可以參考下
    2016-04-04
  • C/C++?Qt?Dialog?對(duì)話框組件應(yīng)用技巧

    C/C++?Qt?Dialog?對(duì)話框組件應(yīng)用技巧

    這篇文章主要介紹了C/C++?Qt?Dialog?對(duì)話框組件應(yīng)用,這里我將總結(jié)本人在開(kāi)發(fā)過(guò)程中常用到的標(biāo)準(zhǔn)對(duì)話框的使用技巧,對(duì)C++?對(duì)話框組件相關(guān)知識(shí)感興趣的朋友一起看看吧
    2021-11-11
  • C++ 簡(jiǎn)單的任務(wù)隊(duì)列詳解

    C++ 簡(jiǎn)單的任務(wù)隊(duì)列詳解

    下面小編就為大家?guī)?lái)一篇C++ 簡(jiǎn)單的任務(wù)隊(duì)列詳解。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2016-12-12

最新評(píng)論