C++读写ini配置文件实现过程详解

 更新时间:2020年07月29日 09:44:09   投稿:yaominghui  
这篇文章主要介绍了C++读写ini配置文件实现过程详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

在Windows的VC下

读ini文件

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

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

上面的=号两边可以加空格,也可以不加

用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不区分大小写,当获得integer <0,那么返回0。lpFileName 必须是绝对路径,因相对路径是以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不区分大小写,若lpAppName为NULL,lpReturnedString缓冲区内装载这个ini文件所有小节的列表,若为lpKeyName=NULL,就在lpReturnedString缓冲区内装载指定小节所有项的列表。lpFileName 必须是绝对路径,因相对路径是以C:\windows\,
返回值:复制到lpReturnedString缓冲区的字符数量,其中不包括那些NULL中止字符。如lpReturnedString缓冲区不够大,不能容下全部信息,就返回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
);
其实就等于,GetPrivateProfileString(NULL,NULL,lpszReturnedBuffer,nSize,lpFileName)

例子:

/* test.ini "="号两边可以加空格,也可以不加
  [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);//没Section,_T("size")没意义了,所以可以写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"  此时“=”两边不会有空格

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

写ini文件

WritePrivateProfileString函数,没有写integer的,可以转成string再写入。

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

自己封装的函数:

获取某一个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;
}
//获取某一个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;
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • C++线程池实现

    C++线程池实现

    线程池是一种并发编程技术,通过预先创建一组线程并复用它们来执行多个任务,避免了频繁创建和销毁线程的开销,本文就来介绍一下C++线程池实现,感兴趣的可以了解一下
    2025-02-02
  • C++中的类型转换static_cast、dynamic_cast、const_cast和reinterpret_cast总结

    C++中的类型转换static_cast、dynamic_cast、const_cast和reinterpret_cas

    这篇文章主要介绍了C++中的类型转换static_cast、dynamic_cast、const_cast和reinterpret_cast总结,需要的朋友可以参考下
    2014-10-10
  • C语言判定一棵二叉树是否为二叉搜索树的方法分析

    C语言判定一棵二叉树是否为二叉搜索树的方法分析

    这篇文章主要介绍了C语言判定一棵二叉树是否为二叉搜索树的方法,结合实例形式综合对比分析了C语言针对二叉搜索树判定的原理、算法、效率及相关实现技巧,需要的朋友可以参考下
    2018-08-08
  • Sublime Text 3 实现C语言代码的编译和运行(示例讲解)

    Sublime Text 3 实现C语言代码的编译和运行(示例讲解)

    下面小编就为大家带来一篇Sublime Text 3 实现C语言代码的编译和运行(示例讲解)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-09-09
  • C++ abs函数实际应用详解

    C++ abs函数实际应用详解

    本文我们来讲C++的abs函数以及实战运用,C++中的abs函数。在C++中使用abs函数要注意存在两种版本,一种是在stdlmb.h中定义的版本,另一个是在cmath头文件中定义的。夷实上在stdlib.h文件是C的函数,而cmath中的是C++版本
    2022-08-08
  • C++类中三大函数详解(构造、析构和拷贝)

    C++类中三大函数详解(构造、析构和拷贝)

    c++三大函数指的是拷贝构造、拷贝赋值、析构函数,下面这篇文章主要给大家介绍了关于C++类中三大函数(构造、析构和拷贝)的相关资料,文中通过实例代码介绍的非常详细,需要的朋友可以参考下
    2023-03-03
  • 一文详解C++11中decltype的使用

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

    这篇文章主要为大家分享了C++11中decltype关键字的使用示例,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2023-07-07
  • C++实现路口交通灯模拟系统

    C++实现路口交通灯模拟系统

    这篇文章主要为大家详细介绍了C++实现路口交通灯模拟系统,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-03-03
  • C++用一棵红黑树同时封装出set与map的实现代码

    C++用一棵红黑树同时封装出set与map的实现代码

    set中存储的一般为键K即可,而map存储的一般都是键值对KV,也就是说他们结构是不同的,那么我们如何才能用一颗红黑树同时封装出set与map两种容器呢,那么接下来我们具体地来研究下STL库中是怎样实现的,并且进行模拟实现,需要的朋友可以参考下
    2024-03-03
  • C++中函数匹配机制详解

    C++中函数匹配机制详解

    大家好,本篇文章主要讲的是C++中函数匹配机制详解,感兴趣的同学赶快来看一看吧,对你有帮助的话记得收藏一下
    2022-02-02

最新评论