配置文件读写

  1. 需求:将文件中的有效内容截取出来,并且放入到一个键值对的数组中
    1. struct ConfigInfo { char key[64] ; char value[64] };
  2. 获取有效行数
  3. 判断当前行是否有效
  4. 解析数据  parseFile
    1. 将有效数据放入到数组中,数组在堆区开辟
  5. 根据key获取value    getInfoByKey
  6. 释放内存  freeSpace

 config.h

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

struct ConfigInfo
{
	char key[64]; //索引值
	char value[64]; //实值
};


//获取有效行数
int getFileLine(char * fileName);

//判断当前行是否有效
int isValidLine(char * str);

//解析文件
void parseFile(char * filePath, int lines, struct ConfigInfo **  configInfo);

//根据索引值 获取 实值 
char * getInfoByKey(char * key, struct ConfigInfo * configInfo, int line);

//释放信息
void freeSpace(struct ConfigInfo * configInfo);

 config.c

#include "config.h"

//获取有效行数
int getFileLine(char * fileName)
{
	FILE * file = fopen(fileName, "r");
	if (file == NULL)
	{
		return -1;
	}

	char buf[1024] = { 0 };
	int lines = 0;
	while (fgets(buf, 1024, file) != NULL)
	{
		//如果是有效行 才统计
		if (isValidLine(buf))
		{
			lines++;
		}

	}
	fclose(file);

	return lines;
}


//判断当前行是否有效
int isValidLine(char * str)
{
	if (str[0] == ' ' || str[0] == '\0' || strchr(str, ':') == NULL)
	{
		return 0; //无效数据 都返回假
	}
	return 1;
}


//解析文件
void parseFile(char * filePath, int lines, struct ConfigInfo **  configInfo)
{
	struct ConfigInfo  * info = malloc(sizeof(struct ConfigInfo) * lines);

	if (info == NULL)
	{
		return;
	}

	FILE * file = fopen(filePath, "r");

	char buf[1024] = { 0 };
	int index = 0;
	while (fgets(buf, 1024, file) != NULL)
	{
		//解析数据  有效数据才解析
		// heroName:aaaa\n
		if (isValidLine(buf))
		{
			memset(info[index].key, 0, 64);
			memset(info[index].value, 0, 64);

			char * pos = strchr(buf, ':'); //pos代表冒号所在位置

			strncpy(info[index].key, buf, pos - buf); //将key截取到 结构体中 
			strncpy(info[index].value, pos + 1, strlen(pos + 1) - 1);//最后-1的原因是不需要截取换行符

			/*printf("key =  %s\n", info[index].key);
			printf("value =  %s\n", info[index].value);*/
			index++;
		}
		memset(buf, 0, 1024);
	}

	*configInfo = info;
}


//根据索引值 获取 实值 
char * getInfoByKey(char * key, struct ConfigInfo * configInfo, int line)
{
	for (int i = 0; i < line; i++)
	{
		if (strcmp(key, configInfo[i].key) == 0)
		{
			return configInfo[i].value;
		}
	}
	return NULL;
}


//释放信息
void freeSpace(struct ConfigInfo * configInfo)
{
	if (configInfo != NULL)
	{
		free(configInfo);
		configInfo = NULL;
	}
}

 main.c

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

struct Person
{
	char a;
	int b;
};

void test03()
{
	char * filePath = "./config.txt";

	int line = getFileLine(filePath);

	printf("文件的有效行数为:%d\n", line);

	struct ConfigInfo * pArray = NULL;

	parseFile(filePath, line, &pArray);

	//测试 根据key 访问value
	printf("东方青龙 = %s\n", getInfoByKey("东方青龙七宿", pArray, line));
	printf("北方玄武 = %s\n", getInfoByKey("北方玄武七宿", pArray, line));
	printf("西方白虎 = %s\n", getInfoByKey("西方白虎七宿", pArray, line));
	printf("南方朱雀 = %s\n", getInfoByKey("南方朱雀七宿", pArray, line));


	//释放内存
	freeSpace(pArray);
	pArray = NULL;



	//文件加密 codeFile( sourceFile ,  destFile )
	// #  35  转为 short
	// 0000 0000 0010 0011   << 4
	// 0000 0010 0011 0000   
	// 1000 0000 0000 0000    |

	// 1000 0010 0011 0000  + 0000 ~ 1111 随机数  rand()%16     0~ 15
	// 1000 0010 0011 1010


	//解密  decodeFile ( sourceFile ,  destFile )
	// 1000 0010 0011 1010  <<1 
	// 000  0010 0011 10100  >> 5
	// 0000 0000  0010 0011
}

int main() {

	test03();

	system("pause");
	return EXIT_SUCCESS;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值