c语言——配置文件读写

c语言——配置文件读写

配置文件读写

  • 文件的读取与关闭
  • 在c++中调用c语言函数
  • 二级指针作函数参数
  • 三级指针作函数参数
  • 二级指针赋值给三级指针
  • 结构体二级指针

代码

头文件

ConfigFile.h

#define _CRT_SECURE_NO_WARNINGS
//防止头文件重复包含
#pragma once

#include<stdio.h>
#include<string.h>
#include<stdlib.h>


struct ConfigInfo
{
	char key[64];
	char val[128];
};


//目的为了在C++中能够调用C写的函数
#ifdef __cplusplus
	extern "C"{
#endif

		//获得文件有效行数
		int getLines_ConfigFile(FILE *file);
		//加载配置文件
		void loadFile_ConfigFile(const char *filePath,char ***fileData,int *lines);
		//解析配置文件
		void parseFile_ConfigFile(char **fileData, int lines, struct ConfigInfo **info);
		//获得指定配置信息
		char* getInfo_ConfigFile(const char *key, struct ConfigInfo *info,int line);
		//释放配置文件信息
		void destroInfo_ConfigFile(struct ConfigInfo *info);
		//判断当前行是否有效
		int isValid_ConfigFile(const char *buf);

#ifdef __cplusplus
	}
#endif

主文件

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

#include"ConfigFile.h"

void test()
{
	char **fileData = NULL;
	int lines = 0;
	struct ConfigInfo *info = NULL;
	//加载配置文件
	loadFile_ConfigFile("./config.ini", &fileData, &lines);
	//解析配置文件
	parseFile_ConfigFile(fileData, lines, &info);
	//printf("IP:%s\n",getInfo_ConfigFile("ip",info,lines));
	//printf("ppp:%s\n", getInfo_ConfigFile("ppp", info, lines));
	//printf("TTT:%s\n", getInfo_ConfigFile("TTT", info, lines));

	//释放配置信息内存
	destroInfo_ConfigFile(info);

}

int main(){

	test();

	system("pause");
	return EXIT_SUCCESS;
}

工具类

ConfigFile.c

判断当前行是否有效

//判断当前行是否有效
int isValid_ConfigFile(const char *buf)
{
	if (buf[0] == '#' || buf[0] == '\n' || strchr(buf,':') == NULL)
	{
		return 0;
	}

	return 1;
}

获得文件有效行数

//获得文件有效行数
int getLines_ConfigFile(FILE *file)
{

	char buf[1024] = { 0 };
	int lines = 0;
	while (fgets(buf,1024,file) != NULL)
	{
		if (!isValid_ConfigFile(buf))
		{
			continue;
		}

		memset(buf, 0, 1024);

		++lines;
	}

	//把文件指针重置到文件的开头
	fseek(file, 0, SEEK_SET);

	return lines;
}

加载配置文件

//加载配置文件
void loadFile_ConfigFile(const char *filePath, char ***fileData, int *line)
{
	
	FILE *file = fopen(filePath, "r");
	if (NULL == file)
	{
		return;
	}

	int lines = getLines_ConfigFile(file);

	//给每行数据开辟内存
	char **temp = malloc(sizeof(char *) * lines);

	char buf[1024] = { 0 };

	int index = 0;

	while (fgets(buf, 1024, file) != NULL)
	{
		//如果返回false
		if (!isValid_ConfigFile(buf))
		{
			continue;
		}

		temp[index] = malloc(strlen(buf) + 1);
		strcpy(temp[index], buf);
		++index;
		//清空buf
		memset(buf, 0, 1024);
	}

	//关闭文件
	fclose(file);


	*fileData = temp; //难点,重点
	*line = lines;
}

解析配置文件

//解析配置文件
void parseFile_ConfigFile(char **fileData, int lines, struct ConfigInfo **info)
{

	struct ConfigInfo *myinfo = malloc(sizeof(struct ConfigInfo) *lines);
	memset(myinfo, 0, sizeof(struct ConfigInfo) *lines);

	for (int i = 0; i < lines; ++i)
	{
		char *pos = strchr(fileData[i], ':');
		
		strncpy(myinfo[i].key, fileData[i], pos - fileData[i]);//读取分隔符(冒号)的前面

		int flag = 0;
		if (fileData[i][strlen(fileData[i]) - 1] == '\n')////最后一行没有换行符
		{
			printf("最后一个是换行!\n");
			flag = 1;
		}
		
		strncpy(myinfo[i].val, pos + 1, strlen(pos + 1) - flag);//读取分隔符(冒号)的前面
		
		printf("key:%s val:%s\n", myinfo[i].key, myinfo[i].val);
	}

	//释放文件信息
	for (int i = 0; i < lines; ++i)
	{
		if (fileData[i]  != NULL)
		{
			free(fileData[i]);
			fileData[i] = NULL;
		}
	}


	*info = myinfo;
}

获得指定配置信息

//获得指定配置信息
char* getInfo_ConfigFile(const char *key, struct ConfigInfo *info,int line)
{
	for (int i = 0; i < line; ++i)
	{
		if (strcmp(key,info[i].key) == 0)
		{
			return info[i].val;
		}
	}

	return NULL;
}

释放配置文件信息

//释放配置文件信息
void destroInfo_ConfigFile(struct ConfigInfo *info)
{

	if (NULL == info)
	{
		return;
	}

	free(info);
	info = NULL;

}

总的代码

#include"ConfigFile.h"

//获得文件有效行数
int getLines_ConfigFile(FILE *file)
{

	char buf[1024] = { 0 };
	int lines = 0;
	while (fgets(buf,1024,file) != NULL)
	{
		if (!isValid_ConfigFile(buf))
		{
			continue;
		}

		memset(buf, 0, 1024);

		++lines;
	}

	//把文件指针重置到文件的开头
	fseek(file, 0, SEEK_SET);

	return lines;
}
//加载配置文件
void loadFile_ConfigFile(const char *filePath, char ***fileData, int *line)
{
	
	FILE *file = fopen(filePath, "r");
	if (NULL == file)
	{
		return;
	}

	int lines = getLines_ConfigFile(file);

	//给每行数据开辟内存
	char **temp = malloc(sizeof(char *) * lines);

	char buf[1024] = { 0 };

	int index = 0;

	while (fgets(buf, 1024, file) != NULL)
	{
		//如果返回false
		if (!isValid_ConfigFile(buf))
		{
			continue;
		}

		temp[index] = malloc(strlen(buf) + 1);
		strcpy(temp[index], buf);
		++index;
		//清空buf
		memset(buf, 0, 1024);
	}

	//关闭文件
	fclose(file);


	*fileData = temp;
	*line = lines;
}


//解析配置文件
void parseFile_ConfigFile(char **fileData, int lines, struct ConfigInfo **info)
{

	struct ConfigInfo *myinfo = malloc(sizeof(struct ConfigInfo) *lines);
	memset(myinfo, 0, sizeof(struct ConfigInfo) *lines);

	for (int i = 0; i < lines; ++i)
	{
		char *pos = strchr(fileData[i], ':');
		
		strncpy(myinfo[i].key, fileData[i], pos - fileData[i]);

		int flag = 0;
		if (fileData[i][strlen(fileData[i]) - 1] == '\n')
		{
			printf("最后一个是换行!\n");
			flag = 1;
		}
		
		strncpy(myinfo[i].val, pos + 1, strlen(pos + 1) - flag);
		
		printf("key:%s val:%s\n", myinfo[i].key, myinfo[i].val);
	}

	//释放文件信息
	for (int i = 0; i < lines; ++i)
	{
		if (fileData[i]  != NULL)
		{
			free(fileData[i]);
			fileData[i] = NULL;
		}
	}


	*info = myinfo;
}
//获得指定配置信息
char* getInfo_ConfigFile(const char *key, struct ConfigInfo *info,int line)
{
	for (int i = 0; i < line; ++i)
	{
		if (strcmp(key,info[i].key) == 0)
		{
			return info[i].val;
		}
	}

	return NULL;
}
//释放配置文件信息
void destroInfo_ConfigFile(struct ConfigInfo *info)
{

	if (NULL == info)
	{
		return;
	}

	free(info);
	info = NULL;

}
//判断当前行是否有效
int isValid_ConfigFile(const char *buf)
{
	if (buf[0] == '#' || buf[0] == '\n' || strchr(buf,':') == NULL)
	{
		return 0;
	}

	return 1;
}

配置文件

配置文件

#这是我的IP地址
ip:127.0.0.1
#这是我的端口
port:8080
#这是我的用户名
username:root
#这是我的密码
password:admin

TTT:OOO

ppp:hello world





uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuua

#1. 加载配置文件

#2. 解析配置文件
#struct ConfigInfo
#{
#	char key[64];
#	char val[128];
#}
aaa:bbb
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章