網站首頁 編程語言 正文
一、介紹
實際項目參數需要保存;有些配置信息需要存放于文件,這里給出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);//將JSON字符串轉換成JSON結構體
if(cjson == NULL) //判斷轉換是否成功
{
printf("cjson error...\r\n");
return (-1);
}
else
{
printf("%s\n",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:%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); //JSON數據結構轉換為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
編譯
結果
原文鏈接:https://blog.csdn.net/u010835747/article/details/125591049
- 上一篇:ubuntu安裝samba文件共享
- 下一篇:樹莓派設置wifi自動連接
相關推薦
- 2023-03-20 C#中如何限制TextBox控件內輸入值的范圍_C#教程
- 2022-06-09 python?Tkinter模塊使用方法詳解_python
- 2023-10-09 注冊頁面編寫
- 2022-05-02 python?異常捕獲詳解流程_python
- 2023-01-18 React手寫redux過程分步講解_React
- 2023-03-25 iOS13適配三指撤銷和文案限長實例詳解_IOS
- 2022-09-22 vrrp協議與keepalived淺析
- 2022-07-10 詳解BlockingQueue阻塞隊列的使用
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支