当前位置: 移动技术网 > IT编程>开发语言>C/C++ > C++读写配置项的基本操作

C++读写配置项的基本操作

2021年01月03日 C/C++ 我要评论
读写配置项,在编程当中是非常常用的东西。读写的数据量很小的时候,就没必要用数据库或者excel之类的东西。今天特意总结下c++、还有qt读写配置项的操作。其实操作非常简单。废话不多说,下面直接上代码。

  读写配置项,在编程当中是非常常用的东西。读写的数据量很小的时候,就没必要用数据库或者excel之类的东西。今天特意总结下c++、还有qt读写配置项的操作。其实操作非常简单。废话不多说,下面直接上代码。

c++ 写配置项

#include <iostream>
#include <windows.h>
using namespace std;

int main()
{
  // 写配置项
  writeprivateprofilestring(l"进程", // 节名称
    l"pid", // 配置项名称
    l"3467", // 欲写入的值
    l".\\config.ini"); // 配置文件名
  writeprivateprofilestring(l"进程", l"pidname", l"6789", l".\\config.ini");
  writeprivateprofilestring(l"线程", l"tid", l"360safe.exe", l".\\config.ini");
  writeprivateprofilestring(l"线程", l"tidname", l"张三", l".\\config.ini");
  getchar();
  return 0;
}

代码执行完会在工程目录下生成config.ini文件。文件里的内容如下图所示。

 c++读配置项

#include <iostream>
#include <windows.h>
using namespace std;

int main()
{
  tchar str1[max_path] = { 0 };
  tchar str2[max_path] = { 0 };
  tchar str3[max_path] = { 0 };
  tchar str4[max_path] = { 0 };
  tchar str5[max_path] = { 0 };
  // 读配置项
  getprivateprofilestring(l"进程", // 配置项节名称
    l"hid", // 配置项名称
    l"呵呵", // 若指定的键不存在,该值作为读取的默认值
    str1, // 一个指向缓冲区的指针,接收读取的字符串
    max_path, // 上面那个缓冲区的大小
    l".\\config.ini"); // 配置文件名
  getprivateprofilestring(l"进程", l"pid", l"呵呵", str2, max_path, l".\\config.ini");
  getprivateprofilestring(l"进程", l"pidname", l"呵呵", str3, max_path, l".\\config.ini");
  getprivateprofilestring(l"线程", l"tid", l"呵呵", str4, max_path, l".\\config.ini");
  getprivateprofilestring(l"线程", l"tidname", l"呵呵", str5, max_path, l".\\config.ini");

  getchar();
  return 0;
}

最后的结果:除了str1得到“呵呵”的值外,其它的都能得到正确的值。另外读配置项还有其它类似api,用到可自行百度或者查看msdn,这里就不一一举例了。

以上就是c++读写配置项的基本操作的详细内容,更多关于c++读写配置项的资料请关注移动技术网其它相关文章!

(0)
打赏 微信扫一扫 微信扫一扫

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。

发表评论

验证码:
移动技术网