实现一个小程序 1. linuxC实现将Edvr.cfg文件解析为一个Config结构体并输出相关内容的程序 文件格式如下: [Section1] Name1=Value1 Name2=Value2 Name3=Value3 [Section2] Name4=Value4 Name5=Value5 Name6=Value6 typedef struct tagKey { // 键名 char *szName; // 键值 char *szValue; } Key; typedef struct tagSection { // 段名 char *szName; // 键数量 int nKeyNum; // 键数组指针 Key *pKeys; } Section; typedef struct tagConfig { // 段数量 int nSectionNum; // 段数组 Section *pSections; } Config; 2. linuxC将Config结构体存为json格式, 保存到文件Config.json的程序 { "Config": { "Section1": { "Name1": "Value1", "Name2": "Value2", "Name3": "Value3" }, "Section2": { "Name4": "Value4", "Name5": "Value5", "Name6": "Value6" } } }

时间: 2024-01-26 15:02:32 浏览: 25
好的,我明白了您的问题。下面是我的回答: 1. 将Edvr.cfg文件解析为Config结构体并输出相关内容的程序: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_SECTION_NUM 10 #define MAX_KEY_NUM 10 #define MAX_NAME_LEN 20 #define MAX_VALUE_LEN 20 typedef struct tagKey { char *szName; char *szValue; } Key; typedef struct tagSection { char *szName; int nKeyNum; Key *pKeys; } Section; typedef struct tagConfig { int nSectionNum; Section *pSections; } Config; Config* LoadConfig(const char* szFile) { char buf[1024]; FILE *fp = fopen(szFile, "r"); if (!fp) { printf("Cannot open file %s!\n", szFile); return NULL; } Config *pConfig = (Config*)malloc(sizeof(Config)); memset(pConfig, 0, sizeof(Config)); Section *pSection = NULL; Key *pKey = NULL; int nLine = 0; while (fgets(buf, 1024, fp)) { nLine++; char *szTrim = buf; while (*szTrim == ' ' || *szTrim == '\t') szTrim++; if (*szTrim == ';' || *szTrim == '#' || *szTrim == '\r' || *szTrim == '\n') continue; if (*szTrim == '[') { char *szEnd = strchr(szTrim+1, ']'); if (!szEnd) { printf("Error in line %d: invalid section!\n", nLine); fclose(fp); free(pConfig); return NULL; } *szEnd = '\0'; pSection = (Section*)malloc(sizeof(Section)); memset(pSection, 0, sizeof(Section)); pSection->szName = strdup(szTrim+1); if (pConfig->nSectionNum >= MAX_SECTION_NUM) { printf("Error in line %d: too many sections!\n", nLine); fclose(fp); free(pSection); free(pConfig); return NULL; } pConfig->pSections[pConfig->nSectionNum++] = *pSection; free(pSection); pSection = &(pConfig->pSections[pConfig->nSectionNum-1]); } else { if (!pSection) { printf("Error in line %d: no section defined!\n", nLine); fclose(fp); free(pConfig); return NULL; } char *szEqual = strchr(szTrim, '='); if (!szEqual) { printf("Error in line %d: invalid key-value pair!\n", nLine); fclose(fp); free(pConfig); return NULL; } *szEqual = '\0'; pKey = (Key*)malloc(sizeof(Key)); memset(pKey, 0, sizeof(Key)); pKey->szName = strdup(szTrim); pKey->szValue = strdup(szEqual+1); if (pSection->nKeyNum >= MAX_KEY_NUM) { printf("Error in line %d: too many keys in section %s!\n", nLine, pSection->szName); fclose(fp); free(pKey); free(pConfig); return NULL; } pSection->pKeys[pSection->nKeyNum++] = *pKey; free(pKey); } } fclose(fp); return pConfig; } void PrintConfig(const Config *pConfig) { int i, j; for (i = 0; i < pConfig->nSectionNum; i++) { printf("[%s]\n", pConfig->pSections[i].szName); for (j = 0; j < pConfig->pSections[i].nKeyNum; j++) { printf("%s=%s\n", pConfig->pSections[i].pKeys[j].szName, pConfig->pSections[i].pKeys[j].szValue); } } } int main() { Config *pConfig = LoadConfig("Edvr.cfg"); if (pConfig) { PrintConfig(pConfig); // Do something with pConfig... // ... free(pConfig); } return 0; } ``` 2. 将Config结构体存为json格式,保存到文件Config.json的程序: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_SECTION_NUM 10 #define MAX_KEY_NUM 10 #define MAX_NAME_LEN 20 #define MAX_VALUE_LEN 20 typedef struct tagKey { char *szName; char *szValue; } Key; typedef struct tagSection { char *szName; int nKeyNum; Key *pKeys; } Section; typedef struct tagConfig { int nSectionNum; Section *pSections; } Config; void WriteJsonString(FILE *fp, const char *szStr) { fputc('"', fp); while (*szStr) { if (*szStr == '\\' || *szStr == '"') { fputc('\\', fp); } fputc(*szStr, fp); szStr++; } fputc('"', fp); } void WriteJsonKey(FILE *fp, const Key *pKey) { WriteJsonString(fp, pKey->szName); fputc(':', fp); WriteJsonString(fp, pKey->szValue); } void WriteJsonSection(FILE *fp, const Section *pSection) { WriteJsonString(fp, pSection->szName); fputc(':', fp); fputc('{', fp); int i; for (i = 0; i < pSection->nKeyNum; i++) { WriteJsonKey(fp, &(pSection->pKeys[i])); if (i < pSection->nKeyNum-1) fputc(',', fp); } fputc('}', fp); } void WriteJsonConfig(FILE *fp, const Config *pConfig) { fputc('{', fp); WriteJsonString(fp, "Config"); fputc(':', fp); fputc('{', fp); int i; for (i = 0; i < pConfig->nSectionNum; i++) { WriteJsonSection(fp, &(pConfig->pSections[i])); if (i < pConfig->nSectionNum-1) fputc(',', fp); } fputc('}', fp); fputc('}', fp); } Config* LoadConfig(const char* szFile) { char buf[1024]; FILE *fp = fopen(szFile, "r"); if (!fp) { printf("Cannot open file %s!\n", szFile); return NULL; } Config *pConfig = (Config*)malloc(sizeof(Config)); memset(pConfig, 0, sizeof(Config)); Section *pSection = NULL; Key *pKey = NULL; int nLine = 0; while (fgets(buf, 1024, fp)) { nLine++; char *szTrim = buf; while (*szTrim == ' ' || *szTrim == '\t') szTrim++; if (*szTrim == ';' || *szTrim == '#' || *szTrim == '\r' || *szTrim == '\n') continue; if (*szTrim == '[') { char *szEnd = strchr(szTrim+1, ']'); if (!szEnd) { printf("Error in line %d: invalid section!\n", nLine); fclose(fp); free(pConfig); return NULL; } *szEnd = '\0'; pSection = (Section*)malloc(sizeof(Section)); memset(pSection, 0, sizeof(Section)); pSection->szName = strdup(szTrim+1); if (pConfig->nSectionNum >= MAX_SECTION_NUM) { printf("Error in line %d: too many sections!\n", nLine); fclose(fp); free(pSection); free(pConfig); return NULL; } pConfig->pSections[pConfig->nSectionNum++] = *pSection; free(pSection); pSection = &(pConfig->pSections[pConfig->nSectionNum-1]); } else { if (!pSection) { printf("Error in line %d: no section defined!\n", nLine); fclose(fp); free(pConfig); return NULL; } char *szEqual = strchr(szTrim, '='); if (!szEqual) { printf("Error in line %d: invalid key-value pair!\n", nLine); fclose(fp); free(pConfig); return NULL; } *szEqual = '\0'; pKey = (Key*)malloc(sizeof(Key)); memset(pKey, 0, sizeof(Key)); pKey->szName = strdup(szTrim); pKey->szValue = strdup(szEqual+1); if (pSection->nKeyNum >= MAX_KEY_NUM) { printf("Error in line %d: too many keys in section %s!\n", nLine, pSection->szName); fclose(fp); free(pKey); free(pConfig); return NULL; } pSection->pKeys[pSection->nKeyNum++] = *pKey; free(pKey); } } fclose(fp); return pConfig; } int main() { Config *pConfig = LoadConfig("Edvr.cfg"); if (pConfig) { FILE *fp = fopen("Config.json", "w"); if (fp) { WriteJsonConfig(fp, pConfig); fclose(fp); } // Do something with pConfig... // ... free(pConfig); } return 0; } ```

相关推荐

最新推荐

recommend-type

HP-Socket编译-Linux

HP-Socket编译-Linux
recommend-type

JavaScript_生活在Discord上的开源社区列表.zip

JavaScript
recommend-type

JavaScript_MultiOn API.zip

JavaScript
recommend-type

JavaScript_简单和完整的React DOM测试工具,鼓励良好的测试实践.zip

JavaScript
recommend-type

JavaScript_成为一个Nodejs开发者.zip

JavaScript
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

可见光定位LED及其供电硬件具体型号,广角镜头和探测器,实验设计具体流程步骤,

1. 可见光定位LED型号:一般可使用5mm或3mm的普通白色LED,也可以选择专门用于定位的LED,例如OSRAM公司的SFH 4715AS或Vishay公司的VLMU3500-385-120。 2. 供电硬件型号:可以使用常见的直流电源供电,也可以选择专门的LED驱动器,例如Meanwell公司的ELG-75-C或ELG-150-C系列。 3. 广角镜头和探测器型号:一般可采用广角透镜和CMOS摄像头或光电二极管探测器,例如Omron公司的B5W-LA或Murata公司的IRS-B210ST01。 4. 实验设计流程步骤: 1)确定实验目的和研究对象,例如车辆或机器人的定位和导航。
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。