一、介绍
实际项目参数需要保存;有些配置信息需要存放于文件,这里给出demo示例
二、原理
1、使用文件存储到文件系统
2、使用json库进行封装和解析
三、示例
config_para.c
#include#include #include #include "cJSON.h" #include const char nConfigFile[] = "configPara.config"; typedef struct{ char ip[32]; unsigned short port; }onenet_http_def; typedef struct{ char devId[32]; char apiKey[128]; char authentication[128]; char productId[32]; }onenet_para_def; typedef struct { onenet_http_def onenethttp; onenet_para_def dataPoint; onenet_para_def gpsPoint; }config_para_def; config_para_def config_para = {0}; static config_para_def default_para = { .onenethttp.ip = "183.230.40.33", .onenethttp.port = 80, .dataPoint.devId = "803857251", .dataPoint.apiKey = "a=FF2LEzpb0EzX=bO=T46TW4Qes=", .dataPoint.productId = "465267", .dataPoint.authentication = "guoguo1233211234567", .gpsPoint.devId = "803857251", .gpsPoint.apiKey = "a=FF2LEzpb0EzX=bO=T46TW4Qes=", .gpsPoint.productId = "465267", .gpsPoint.authentication = "guoguo1233211234567", }; char read_buff[2048] = {0}; int checkefileexist(char *filename) { if((access(filename, F_OK)) == -1) { printf("%s file not exist %s line:%dn", filename, __FILE__, __LINE__); return (-1); } return 0; } int read_file(char *filename, char *buff, int len) { FILE *fp; int ret = 0; fp = fopen(filename, "r"); if(fp == NULL) { printf("打开文%s件失败n", filename); return (-1); } ret = fread(buff, 1, len, fp); fclose(fp); return ret; } int jsonparse(char *buff, int len) { cJSON *cjson = NULL, *onenethttp_json = NULL, *dataPoint_json = NULL,*gpsPoint_json = NULL; char *str_tmp = NULL; int int_tmp = 0; cjson = cJSON_Parse(buff);//将JSON字符串转换成JSON结构体 if(cjson == NULL) //判断转换是否成功 { printf("cjson error...rn"); return (-1); } else { printf("%sn",cJSON_Print(cjson));//打包成功调用cJSON_Print打印输出 } printf("json parse:n"); onenethttp_json = cJSON_GetObjectItem(cjson,"onenethttp"); //解析对象 str_tmp = cJSON_GetObjectItem(onenethttp_json, "ip")->valuestring; //解析对象中的字符串 strcpy(config_para.onenethttp.ip, str_tmp); printf("ip:%sn", str_tmp); free(str_tmp); int_tmp = cJSON_GetObjectItem(onenethttp_json, "port")->valueint; //解析对象中的字符串 config_para.onenethttp.port = int_tmp; printf("port:%dn", int_tmp); dataPoint_json = cJSON_GetObjectItem(cjson,"dataPoint"); //解析对象 str_tmp = cJSON_GetObjectItem(dataPoint_json, "devId")->valuestring; //解析对象中的字符串 strcpy(config_para.dataPoint.devId, str_tmp); printf("dataPoint devId:%sn", str_tmp); free(str_tmp); str_tmp = cJSON_GetObjectItem(dataPoint_json, "apiKey")->valuestring; //解析对象中的字符串 strcpy(config_para.dataPoint.apiKey, str_tmp); printf("dataPoint apiKey:%sn", str_tmp); free(str_tmp); gpsPoint_json = cJSON_GetObjectItem(cjson,"gpsPoint"); //解析对象 str_tmp = cJSON_GetObjectItem(gpsPoint_json, "devId")->valuestring; //解析对象中的字符串 strcpy(config_para.gpsPoint.devId, str_tmp); printf("gpsPoint devId:%sn", str_tmp); free(str_tmp); str_tmp = cJSON_GetObjectItem(gpsPoint_json, "apiKey")->valuestring; //解析对象中的字符串 strcpy(config_para.gpsPoint.apiKey, str_tmp); printf("gpsPoint apiKey:%sn", str_tmp); free(str_tmp); return 0; } int read_config() { int ret = 0; ret = checkefileexist(nConfigFile); if(ret != 0) { config_para = default_para; printf("use default para %s line:%dn", __FILE__, __LINE__); } else { memset(read_buff, 0, 2048); ret = read_file(nConfigFile, read_buff, 2048); if(ret == -1) { printf("read file error %s line:%dn", __FILE__, __LINE__); config_para = default_para; return (-1); } else { printf("len: %d nfile %sn", ret, read_buff); ret = jsonparse(read_buff, 2048); if(ret == -1) config_para = default_para; } } return 0; } int write_file(char *filename, char *buff, int len) { FILE *fp; int ret; fp = fopen(filename, "w"); if(fp == NULL) { printf("打开文%s件失败n",filename); return 0; } ret = fwrite(buff, 1, len, fp); if(ret != len) { printf("写入%s文件失败",filename); } fclose(fp); } int write_config(config_para_def config_para) { cJSON *config_json = cJSON_CreateObject(); //创建一个对象 cJSON *onenethttp_json = cJSON_CreateObject(); //创建一个对象 cJSON_AddStringToObject(onenethttp_json,"ip", config_para.onenethttp.ip); //添加字符串 cJSON_AddNumberToObject(onenethttp_json,"port", config_para.onenethttp.port); //添加整型数字 cJSON_AddItemToObject(config_json, "onenethttp", onenethttp_json); cJSON *dataPoint_json = cJSON_CreateObject(); //创建一个对象 cJSON_AddStringToObject(dataPoint_json,"devId", config_para.dataPoint.devId); //添加字符串 cJSON_AddNumberToObject(dataPoint_json,"apiKey", config_para.dataPoint.apiKey); //添加整型数字 cJSON_AddItemToObject(config_json, "onenethttp", dataPoint_json); cJSON *gpsPoint_json = cJSON_CreateObject(); //创建一个对象 cJSON_AddStringToObject(gpsPoint_json,"devId", config_para.gpsPoint.devId); //添加字符串 cJSON_AddNumberToObject(gpsPoint_json,"apiKey", config_para.gpsPoint.apiKey); //添加整型数字 cJSON_AddItemToObject(config_json, "onenethttp", gpsPoint_json); char *json_data = cJSON_Print(config_json); //JSON数据结构转换为JSON字符串 printf("%sn", json_data);//输出字符串 write_file(nConfigFile, json_data, strlen(json_data)); cJSON_Delete(config_json);//清除结构体 return 0; }
config_para.h
#ifndef __CONFIG_PARA_H
#define __CONFIG_PARA_H
typedef struct{
char ip[32];
unsigned short port;
}onenet_http_def;
typedef struct{
char devId[32];
char apiKey[128];
char authentication[128];
char productId[32];
}onenet_para_def;
typedef struct {
onenet_http_def onenethttp;
onenet_para_def dataPoint;
onenet_para_def gpsPoint;
}config_para_def;
extern config_para_def config_para;
int read_config();
int write_config(config_para_def config_para);
#endif
configPara.config
{
"onenethttp": {
"ip": "183.230.40.33",
"port": 80
},
"dataPoint": {
"devId": "803857251",
"apiKey": "a=FF2LEzpb0EzX=bO=T46TW4Qes=",
"productId": "465267",
"authentication": "guoguo1233211234567"
},
"gpsPoint": {
"devId": "803857251",
"apiKey": "a=FF2LEzpb0EzX=bO=T46TW4Qes=",
"productId": "465267",
"authentication": "guoguo1233211234567"
}
}
test.c
#include#include "config_para.h" int main() { read_config(); return 0; }
编译脚本
buildtest.sh
gcc test.c cJSON.c config_para.c -o test -lm echo "main run..." ./test
编译
结果



