一、介绍
实际项目参数需要保存;有些配置信息需要存放于文件,这里给出demo示例
二、原理
1、使用文件存储到文件系统
2、使用json库进行封装和解析
三、示例
config_para.c
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include "cJSON.h"
#include <fcntl.h>
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:%d\n", 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);
if(cjson == NULL)
{
printf("cjson error...\r\n");
return (-1);
}
else
{
printf("%s\n",cJSON_Print(cjson));
}
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:%s\n", str_tmp);
free(str_tmp);
int_tmp = cJSON_GetObjectItem(onenethttp_json, "port")->valueint;
config_para.onenethttp.port = int_tmp;
printf("port:%d\n", 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:%s\n", str_tmp);
free(str_tmp);
str_tmp = cJSON_GetObjectItem(dataPoint_json, "apiKey")->valuestring;
strcpy(config_para.dataPoint.apiKey, str_tmp);
printf("dataPoint apiKey:%s\n", 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:%s\n", str_tmp);
free(str_tmp);
str_tmp = cJSON_GetObjectItem(gpsPoint_json, "apiKey")->valuestring;
strcpy(config_para.gpsPoint.apiKey, str_tmp);
printf("gpsPoint apiKey:%s\n", 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:%d\n", __FILE__, __LINE__);
}
else
{
memset(read_buff, 0, 2048);
ret = read_file(nConfigFile, read_buff, 2048);
if(ret == -1)
{
printf("read file error %s line:%d\n", __FILE__, __LINE__);
config_para = default_para;
return (-1);
}
else
{
printf("len: %d \nfile %s\n", 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);
printf("%s\n", 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 <stdio.h>
#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
编译

结果

