实现一个小程序 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 09:02:32 浏览: 119
好的,我明白了您的问题。下面是我的回答:
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;
}
```
阅读全文