c++读取ini格式配置文件
时间: 2023-08-01 11:14:12 浏览: 84
在C++中读取ini格式的配置文件,可以使用Windows API中的GetPrivateProfileString函数。
首先,需要包含Windows.h头文件。
然后,使用GetPrivateProfileString函数来读取配置文件中的值。
下面是一个示例代码:
```cpp
#include <Windows.h>
int main()
{
TCHAR iniFilePath[] = TEXT("config.ini");
TCHAR appName[] = TEXT("App");
TCHAR keyName[] = TEXT("Key");
TCHAR defaultValue[] = TEXT("");
TCHAR value[256] = { 0 };
GetPrivateProfileString(appName, keyName, defaultValue, value, 256, iniFilePath);
// do something with value
return 0;
}
```
其中,iniFilePath是配置文件的路径;appName是配置项所属的节名;keyName是配置项的名称;defaultValue是默认值,当配置项不存在时返回该值;value是读取到的配置项的值。
GetPrivateProfileString函数的返回值为读取到的字符串长度(不包括NULL字符)。
注意,GetPrivateProfileString函数只能读取ANSI编码的ini文件。如果ini文件是Unicode编码的,可以使用GetPrivateProfileStringW函数。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)