读取配置文件
1. 文件中按照键值对方式 存放了有效的信息需要解析出来
2. 创建 config.h 和 config.c做配置文件读操作
3. 获取有效信息的行数 getFileLines
4. 判断字符串是否是有效行 int isValidLines(char *str)
5. 解析文件到配置信息数组中
6. 通过key获取value值
7. 释放内存
5. 解析文件到配置信息数组中
void parseFile(char * filePath, int lines , struct ConfigInfo ** configinfo);
6. 通过key获取value值
char * getInfoByKey(char * key, struct ConfigInfo * configinfo, int len);
7. 释放内存
void freeConfigInfo(struct ConfigInfo * configinfo);
config.h
#pragma once
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <Windows.h>
//配置结构体
struct ConfigInfo
{
char key[64];
char value[64];
};
//获取有效行数
int getFileLines(char* filePath);
//检测当前行是否是有效信息
int isValidLines(char* str);
//解析文件
void parseFile(char* filePath, int lines, struct ConfigInfo** configinfo);
//根据key获取对应value
char* getInfoByKey(char* key, struct ConfigInfo* configinfo, int len);
//释放内存
void freeConfigInfo(struct ConfigInfo* configinfo);
config.c
#include "config.h"
int getFileLines(char* filePath)
{
FILE * file = fopen(filePath, "r");
if (file == NULL)
{
return -1;
}
char buf[1024] = {
0 };