Windows API 读写.ini文件相关函数

前言

        .ini文件常作为软件启动设置信息存储的文件,内容与jsonl有一定的相似,主要都是以键值对的形式进行数据存储。本文主要介绍Windows系统为我们提供的读写.ini文件的函数——---GetPrivateProfileInt和WritePrivateProfileString

正文

ini文件

        ini文件内容主要格式:

         节:[section]

         参数:键 = 值 (键值对)

       示例:

[Section1 Name]

KeyName1=value1
KeyName2=value2

[Section2 Name]

KeyName3=value3
KeyName4=value4

 KeyName1、KeyName2属于节 Section1,同理  KeyName3、KeyName4属于节 Section2;

 读ini文件:GetPrivateProfileInt

        读ini文件就需要使用Windows API-- GetPrivateProfileInt函数

        函数原型

UINT GetPrivateProfileInt(  
  LPCTSTR lpAppName, // 节的名称  
  LPCTSTR lpKeyName, // 键的名称  
  INT     nDefault,  // 如果找不到键,则返回的默认值  
  LPCTSTR lpFileName // 初始化文件的名称  
);

函数参数类型 :LPCTSTR 

        它是一个宏定义,由几个部分组成:

        1.LP表示long pointer  : 长指针类型

        2.C表示const : 指针指向的内容为常量

        3.T代表在Win32环境中可能存在的_T宏,用于兼容Unicode和ANSI字符串;(具体不太清楚)

        4.STR表示这个变量是一个字符串

函数参数类型 :UINT

        无符号整型

函数参数类型 :INT

        整型

示例:

[Window]  
Width=800  
Height=600  
  
[Colors]  
BackgroundColor=12345  
ForegroundColor=65432
int main() {  
    // 初始化变量来存储从INI文件中读取的值  
    int width, height, backgroundColor;  
  
    // 从settings.ini文件的[Window]节中读取Width和Height  
    width = GetPrivateProfileInt("Window", "Width", 800, "settings.ini"); // 如果找不到Width,则默认为800  
    height = GetPrivateProfileInt("Window", "Height", 600, "settings.ini"); // 如果找不到Height,则默认为600  
  
    // 注意:BackgroundColor在这个例子中被当作整数处理,但在实际中它可能是颜色代码字符串  
    // 这里我们假设INI文件中的BackgroundColor已经是一个整数  
    backgroundColor = GetPrivateProfileInt("Colors", "BackgroundColor", 0, "settings.ini"); // 如果找不到BackgroundColor,则默认为0  
  
    // 输出读取的值  
    std::cout << "Window Width: " << width << std::endl;  
    std::cout << "Window Height: " << height << std::endl;  
    std::cout << "Background Color (Integer): " << backgroundColor << std::endl;  
  
    return 0;  
}

注意事项

        1.GetPrivateProfileInt只能检索特定节(section)或键(key)的值。

        2.返回值是一个无符号整数(UINT),它对应于 INI 文件中指定键名后面的字符串的整数等效值。如果 INI 文件中找到了指定的节名和键名,并且该键名的值可以被成功转换为整数,那么函数就会返回这个整数值。

写ini文件:WritePrivateProfileString

         函数原型

BOOL WritePrivateProfileString(  
  LPCTSTR lpAppName,   // 节的名称  
  LPCTSTR lpKeyName,   // 键的名称  
  LPCTSTR lpString,    // 与键关联的字符串值  
  LPCTSTR lpFileName   // 初始化文件的名称  
);

         示例

int main() {  
    // 要写入的 INI 文件路径  
    const char* iniFilePath = "settings.ini";  
  
    // 写入 [Window] 节下的 Width 和 Height  
    if (!WritePrivateProfileString("Window", "Width", "800", iniFilePath)) {  
        std::cerr << "Failed to write Width to INI file." << std::endl;  
    }  
    if (!WritePrivateProfileString("Window", "Height", "600", iniFilePath)) {  
        std::cerr << "Failed to write Height to INI file." << std::endl;  
    }  
  
    // 写入 [Colors] 节下的 BackgroundColor 和 ForegroundColor  
    // 注意:虽然颜色代码通常是十六进制字符串,但这里我们仍然以字符串形式写入  
    // 在实际应用中,你可能需要将这些十六进制字符串转换为整数(如果适用),然后再写回为十六进制字符串  
    if (!WritePrivateProfileString("Colors", "BackgroundColor", "00FF00", iniFilePath)) { // 绿色  
        std::cerr << "Failed to write BackgroundColor to INI file." << std::endl;  
    }  
    if (!WritePrivateProfileString("Colors", "ForegroundColor", "FF0000", iniFilePath)) { // 红色  
        std::cerr << "Failed to write ForegroundColor to INI file." << std::endl;  
    }  
  
    // 如果需要,可以检查 INI 文件以验证写入是否成功  
    // 这里我们仅打印一条消息表示写入操作已完成  
    std::cout << "INI file has been updated successfully." << std::endl;  
  
    return 0;  
}

         注意事项

         WritePrivateProfileString 函数总是返回非零值以表示成功,除非出现了某种严重的错误

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一直在找资料的菜鸟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值