21.加载配置文件

本文介绍如何在C++中使用C语言编写的函数,通过示例代码展示了如何加载和解析配置文件,并提供了关键函数的实现,如获取文件有效行数、加载配置文件、解析配置文件等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

为了再C++中使用c语言的方法,需要在c的代码中添加下面的信息

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

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

#ifdef __cplusplus
	}
#endif

读取配置文件配置.cpp


#include "iostream"
#include "string.h"
#include "stdlib.h"
#include"ConfigFile.h"
using namespace std;



void test01()
{
	char **fileData = NULL;
	int lines = 0;
	struct ConfigInfo *info = NULL;
	//加载配置文件
	loadFile_ConfigFile("./config.ini", &fileData, &lines);
	//解析配置文件
	parseFile_ConfigFile(fileData, lines, &info);
	cout << "IP:" << getInfo_ConfigFile("ip", info, lines) << endl;
	cout << "ppp:" << getInfo_ConfigFile("ppp", info, lines) << endl;
	cout << "TTT:" <<  getInfo_ConfigFile("TTT", info, lines) << endl;

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


int main()
{
	test01();
	cout << "截取的字符串是=====" << endl;

	system("pause");
	return EXIT_SUCCESS;
}

ConfigFile.h准备调用函数的头文件

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

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<iostream>
using namespace std;


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

ConfigFile.cpp函数的实现

#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 = (char**) 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] = (char*) 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 = (struct ConfigInfo *) 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')
		{
			cout << "最后一个是换行" << endl;
			flag = 1;
		}

		strncpy(myinfo[i].val, pos + 1, strlen(pos + 1) - flag);

		cout << "key:" << myinfo[i].key << "      " << "val:" << myinfo[i].val << endl;
	}

	//释放文件信息
	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;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值