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.
posted @ 2021-05-29 11:53  jqdy  阅读(1136)  评论(0编辑  收藏  举报