配置文件

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<<num<<endl;

    cout<<str<<endl;

    char str2[50];

    num = GetPrivateProfileSectionA("section", str2, sizeof(str2), strPath.c_str());

    cout<<num<<endl;

    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 lpPath = new char[MAX_PATH];

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

delete [] lpPath;

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

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

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

原文地址:https://www.cnblogs.com/gd-luojialin/p/10962974.html