当前位置: 首页 > news >正文

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 函数总是返回非零值以表示成功,除非出现了某种严重的错误


http://www.mrgr.cn/news/15166.html

相关文章:

  • 【Bigtop】Ambari2.8.0编译及安装
  • 华为集合通信库开源了!HCCL开源链接、架构、拓扑算法、常用接口
  • 密码学基础
  • Vue3 组件方法暴露给其他组件调用 defineExpose使用
  • 玩转云服务:Oracle Cloud甲骨文永久免费云主机配置指南(续)
  • [英语单词] feedback
  • 设计模式-结构型模式-组合模式
  • 前端web开发HTML+CSS3+移动web(0基础,超详细)——第5天
  • C++ 设计模式——迭代器模式
  • 数据库视图的使用
  • OprnHarmony驱动开发:外设器件HDF驱动移植规范
  • Python和PySide6实现分别实现tcp通信。
  • SpringBoot 实战:SpringBoot整合Flink CDC,实时追踪mysql数据变动
  • SprinBoot+Vue餐厅点餐系统的设计与实现
  • 深度学习速通系列:贝叶思SVM
  • 操作系统:哪些函数属于系统调用?
  • DataWhale夏令营:Task2:建模方案解读与进阶
  • 微服务中间件之Nacos-安装篇
  • useeffect和uselayout区别
  • docker安装minio