文章目录
一、写出或更新配置文件二、读取配置文件一、写出或更新配置文件
写出或更新配置文件 :
首先 , 使用 字符数组 存储 键值对 信息 ;
// 写出 或 更新 的配置项// 数组声明会后 , 注意先进行初始化为 0 操作 , 否则其中的数据可能是随机的char key[256] = {0};// Key 键char value[256] = {0}; // Value 值
然后 , 通过scanf
获取 键值对 信息 ;
// 提示输入 Key 键printf("\nPlease Input Key : ");// 将 Key 存储到 name 字符串数组中scanf("%s", key);// 提示输入 Value 值printf("\nPlease Input Value : ");// 将 Value 值 存储到 value 字符串数组中scanf("%s", value);
最后 , 调用write_or_update_config_file
函数 , 传入 文件名 , 要 写出 或 更新的 键值对信息 ;
// 向 D:/File/config.ini 写出或更新 键值对 信息ret = write_or_update_config_file(CONFIG_FILE_NAME /*in*/, key /*in*/, value/*in*/,strlen(value) /*in*/);
代码示例 :
/*** @brief write_update_config* 启动 写出 / 更新 配置项 模块 , 执行 写出 / 更新 配置项操作* @return*/int write_update_config(){// 局部变量 返回值 , 用于表示程序状态int ret = 0;// 写出 或 更新 的配置项// 数组声明会后 , 注意先进行初始化为 0 操作 , 否则其中的数据可能是随机的char key[256] = {0};// Key 键char value[256] = {0}; // Value 值// 提示输入 Key 键printf("\nPlease Input Key : ");// 将 Key 存储到 name 字符串数组中scanf("%s", key);// 提示输入 Value 值printf("\nPlease Input Value : ");// 将 Value 值 存储到 value 字符串数组中scanf("%s", value);// 向 D:/File/config.ini 写出或更新 键值对 信息ret = write_or_update_config_file(CONFIG_FILE_NAME /*in*/, key /*in*/, value/*in*/,strlen(value) /*in*/);// 判定执行中是否出错if (ret != 0){printf("error : write_or_update_config : %d \n", ret);return ret;}// 打印成功插入的键值对printf("Input :%s = %s Success !\n", key , value);return ret;}
二、读取配置文件
读取配置文件 :
首先 , 使用 字符数组 存储 键值对 信息 , 声明相关字符数组 ;
// 读取 的配置项// 数组声明会后 , 注意先进行初始化为 0 操作 , 否则其中的数据可能是随机的char key[256] = {0};// Key 键char value[256] = {0}; // Value 值
然后 , 通过scanf
获取 键值对 信息 ;
// 提示输入 Key 键printf("\nPlease Input Key : ");// 将 Key 存储到 name 字符串数组中scanf("%s", key);
最后 , 调用read_config_file
函数 , 传入 文件名称 , 键 字符串 , 值的接收数组 , 以及 写入 Value 值 的长度 ;
// 从 D:/File/config.ini 读取 键值对 信息ret = read_config_file(CONFIG_FILE_NAME /*in*/, key /*in*/, value/*in*/, &value_len /*out*/);
代码示例 :
/*** @brief read_config* 从配置文件中 , 读取配置文件 键值对 信息* @return*/int read_config(){// 局部变量 返回值 , 用于表示程序状态int ret = 0;// 读取 的配置项// 数组声明会后 , 注意先进行初始化为 0 操作 , 否则其中的数据可能是随机的char key[256] = {0};// Key 键char value[256] = {0}; // Value 值// 值 Value 的长度int value_len = 0;// 提示输入 Key 键printf("\nPlease Input Key : ");// 将 Key 存储到 name 字符串数组中scanf("%s", key);// 从 D:/File/config.ini 读取 键值对 信息ret = read_config_file(CONFIG_FILE_NAME /*in*/, key /*in*/, value/*in*/, &value_len /*out*/);if (ret != 0){printf("error : read_config : %d \n", ret);return ret;}// 打印查询结果printf("Get Value Success , Value = %s \n", value);}