Windows API 读写.ini文件

Posted jqdy

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Windows API 读写.ini文件相关的知识,希望对你有一定的参考价值。

  .ini文件是windows程序的配置文件,其结构为:

;以分号开头的行为注释行
;[]括起来的行为章节名,后续的”key-value对“都属该章节
[section 1]
;等号前面的相当于key,等号后面的相当于value
User Name=John
User Id=1
[section 2]
Position=Position 1

  相关函数均位于Winbase.h中,函数命名上均包含***PrivateProfile***,例如:GetPrivateProfileInt(),意思是读取.ini文件中的一个整数。

  1. 从动作上分成两类:
    • PrivateProfile前面的是Get,都是读取
    • PrivateProfile前面的是Write,都是写入
  2. 从操作对象上分成5类:
    • PrivateProfile后面的是Int,表示读出指定章节的指定key对应的整数
    • PrivateProfile后面的是Section,表示读出的是指定章节的所有key-value对
    • PrivateProfile后面的是SectionNames,表示读出的是所有的章节名
    • PrivateProfile后面的是String,表示读出的是指定章节、指定key的value
    • PrivateProfile后面的是Struct,表示按照一定的结构,将指定章节的所有key对应的value一并读出来
  3. 常用参数命名的含义,例如
UINT GetPrivateProfileInt(
  LPCTSTR lpAppName, //指定的章节名
  LPCTSTR lpKeyName, //指定的key
  INT     nDefault,  //如果未发现这个key,返回该缺省值
  LPCTSTR lpFileName //.ini文件的绝对路径+文件名。这里要注意:必须是.ini文件的绝对路径+文件名.ini
);    

  读取函数列表:

GetPrivateProfileInt

Retrieves an integer associated with a key in the specified section of an initialization file.
GetPrivateProfileIntA

Retrieves an integer associated with a key in the specified section of an initialization file.
GetPrivateProfileIntW

Retrieves an integer associated with a key in the specified section of an initialization file.
GetPrivateProfileSection

Retrieves all the keys and values for the specified section of an initialization file.
GetPrivateProfileSectionA

Retrieves all the keys and values for the specified section of an initialization file.
GetPrivateProfileSectionNames

Retrieves the names of all sections in an initialization file.
GetPrivateProfileSectionNamesA

Retrieves the names of all sections in an initialization file.
GetPrivateProfileSectionNamesW

Retrieves the names of all sections in an initialization file.
GetPrivateProfileSectionW

Retrieves all the keys and values for the specified section of an initialization file.
GetPrivateProfileString

Retrieves a string from the specified section in an initialization file.
GetPrivateProfileStringA

Retrieves a string from the specified section in an initialization file.
GetPrivateProfileStringW

Retrieves a string from the specified section in an initialization file.
GetPrivateProfileStruct

Retrieves the data associated with a key in the specified section of an initialization file.
GetPrivateProfileStructA

Retrieves the data associated with a key in the specified section of an initialization file.
GetPrivateProfileStructW

Retrieves the data associated with a key in the specified section of an initialization file.

  写入函数列表:

 

WritePrivateProfileSectionA

Replaces the keys and values for the specified section in an initialization file.
WritePrivateProfileSectionW

Replaces the keys and values for the specified section in an initialization file.
WritePrivateProfileStringA

Copies a string into the specified section of an initialization file.
WritePrivateProfileStringW

Copies a string into the specified section of an initialization file.
WritePrivateProfileStructA

Copies data into a key in the specified section of an initialization file. As it copies the data, the function calculates a checksum and appends it to the end of the data.
WritePrivateProfileStructW

Copies data into a key in the specified section of an initialization file. As it copies the data, the function calculates a checksum and appends it to the end of the data.

例如:test.ini文件内容如下

;[Sql Server] section define the argument of Database connection string.
[Sql Server]
SqlServerIP=192.168.1.1
SqlServerPort=1433
InstanceName=QC
Database=Test
UserId=**
Password=****
;[TCP Server] section define the argument of TCP Server.
[Tcp Server]
TcpServerPort=5000
TcpLiseningAmount=30
;Log File is the path and filename used to record information when Tcp Server running.
[Log]
LogFileName=d:\\log.txt

读取相关参数的代码片段:(test.ini与test.exe位于同一目录下)

#include <Windows.h>
#include <filesystem>
#include <string>
......
    //m_buffer是已分配内存的字符串指针
    std::string currentPath = std::filesystem::current_path().string() + "\\\\test.ini" ;
    GetPrivateProfileStringA("Sql Server", "SqlServerIP", "192.168.1.2", m_buffer, 255, currentPath.c_str());

 

以上是关于Windows API 读写.ini文件的主要内容,如果未能解决你的问题,请参考以下文章

C#中读写INI文件

Windows中如何读写INI文件

引用“kernel32”读写ini配置文件

C# 读写Ini文件

[Python3]INI文件读写

ConfigParser 读写配置文件