栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

配置文件解析

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

配置文件解析

txt版配置文件解析
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
using namespace std;

char *file_path = "D:/Users/clg2021/Desktop/game.txt";
char *tt[] = { 
	"#英雄的Id",
    "heroId : 1",
    "#英雄的姓名",
	"heroname : 德玛西亚",
	"#英雄的攻击力",
	"heroAtk : 1000",
	"#英雄的防御力",
	"heroDef : 500",
	"#英雄的简介",
	"heroInfo : 前排坦克"
};
char**allocate_memory(int n)
{
	if (n < 0)return NULL;
	char**temp = (char**)malloc(sizeof(char*)*n);
	if (temp == NULL)
	{
		return NULL;
	}
	for (int i = 0; i < n; ++i)
	{
		temp[i] =(char*) malloc(sizeof(char*) * 50);
		strcpy(temp[i], tt[i]);
	}
	return temp;
}
void write_data()
{
	FILE*fp = fopen(file_path, "w+");
	if (fp == NULL)
	{
		perror("open");
		return;
	}
	int n = sizeof(tt) / sizeof(char*);
	char **p = allocate_memory(n);
	for (int i = 0; i < n; ++i)
	{
		char *temp = strcat(p[i], "n");
		fputs(temp, fp);
	}

	for (int i = 0; i < n; ++i)
	{
		free(p[i]);
		p[i] = NULL;
	}
	free(p);
	fclose(fp);
}

struct ConfigInfo
{
	char *key;
	char *value;
};

//检查是否为有效行数
int isValidLine(char*buf)
{
	if (buf[0] == '0' || buf[0] == '' || strchr(buf, ':') == NULL)
	{
		return 0;// 如果行无限 返回假
	}
	return 1;

}

//获取文件数据的行数
int get_fileLine(const char*filePath)
{
	FILE * file = fopen(filePath, "r");
	char buf[1024] = { 0 };
	int lines = 0;
	while (fgets(buf, 1024, file) != NULL)
	{
		if (isValidLine(buf))
		{
			lines++;
		}
		memset(buf, 0, 1024);
	}

	fclose(file);

	return lines;
}
//解析文件
void parseFile(const char*file_path, int lines, struct ConfigInfo**config)
{
	struct ConfigInfo*pConfig = (struct ConfigInfo*) malloc(sizeof(struct ConfigInfo)*lines);
	if (pConfig == NULL)
	{
		return;
	}
	FILE*fp = fopen(file_path, "r");
	if (fp == NULL)
	{
		perror("open");
		return;
	}
	char buf[1024] = { 0 };
	int index = 0;
	while (fgets(buf, 1024, fp) != NULL)
	{
		if (isValidLine(buf))
		{
			pConfig[index].key = (char*)malloc(sizeof(char)*64);
			memset(pConfig[index].key, 0, sizeof(char) * 64);
			pConfig[index].value = (char*)malloc(sizeof(char) * 64);
			memset(pConfig[index].value, 0, sizeof(char) * 64);

			char*pos = strchr(buf, ':');
			strncpy(pConfig[index].key, buf, pos - buf);
			strncpy(pConfig[index].value, pos+1, strlen(pos + 1)-1);
			index++;
		}
		memset(buf, 0, 1024);
	}

	*config = pConfig;
}
void freeConfig(struct ConfigInfo**config)
{
	if (config == NULL)
	{
		return;
	}
	struct ConfigInfo*temp = *config;
	if (temp->key != NULL)
	{
		free(temp->key);
		temp->key = NULL;
	}
	if (temp->value != NULL)
	{
		free(temp->value);
		temp->value = NULL;
	}
	free(temp);
}


void read_data()
{
	int line = get_fileLine(file_path);
	cout << "line:" << line << endl;
	struct ConfigInfo * config = NULL;
	parseFile(file_path, line, &config);
	for (int i = 0; i < line; ++i)
	{
		cout << config[i].key << "  " << config[i].value << endl;
	}


}
int main()
{
	//写入数据
	//write_data();
	//解析数据
	read_data();
	//char *buf = "sdd:123";
	//char*pos = strchr(buf, ':');
	//char te1[20]="", te2[20]="";
	//strncpy(te1, buf, pos - buf);
	//strncpy(te2, pos + 1, strlen(pos + 1));
	//cout << pos << " " << pos - buf << " " << pos + 1 << endl;
	//cout << te1 << "    " << te2 << endl;



	system("pause");
	return 0;
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/737722.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号