C语言 实现配置文件的读写

首先在config.h中作函数声明

#pragma once

#ifdef _cplusplus
extern "C"
{
	//函数的声明
	int ReadCfgFile(char* file, char* key, char* value, int* len);
	int WriteCfgFile(char* file, char* key, char* value, int len);

}

#endif

config.c中写相关功能函数

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define SIZE 1024*8

int trimSpace(char* inbuf, char* outbuf)
{
	if (inbuf == NULL || outbuf == NULL)
	{
		return -1;
	}

	char* p = inbuf;
	int begin = 0;
	int end = strlen(p) - 1;
	int n = 0;

	if (end < 0)
	{
		return -2;
	}

	//从左往右移动,如果当前字符为空,且没有结束
	while (isspace(p[begin]) && p[begin] != 0)
	{
		begin++;
	}

	//从右往左移
	while (isspace(p[end]) && end > 0)
	{
		end--;
	}

	if (end == 0)
	{
		return -2;
	}

	n = end - begin + 1;

	strncpy(outbuf, p + begin, n);
	outbuf[n] = 0;

	return 0;
}

int ReadCfgFile(char* file, char* key, char* value, int* len)
{
	FILE* fp = NULL;
	int ret = 0;
	char lineBuf[1024] = { 0 };
	char* p = NULL;
	int flag = 0;//0代表没找到,1代表找到

	//打开文件
	fp = fopen(file, "r+");
	if (fp == NULL)
	{
		ret = -1;
		goto End;
	}

	while (1)
	{
		//读取1行
		if (fgets(lineBuf, sizeof(lineBuf), fp) == NULL)
		{
			break;
		}

		//lineBuf = "player = 1"
		//找key
		p = strstr(lineBuf, key);
		if (p == NULL)//没有
		{
			continue;//跳出本次循环
		}

		//重新设置起点
		//"player = 1" -> "1"
		p = p + strlen(key);

		//找“=”
		p = strstr(p, "=");
		if (p == NULL)
		{
			continue;
		}

		//重新设置起点
		//" = 1" -> "1"
		p = p + strlen("=");
		
		//获取非空字符
		ret = trimSpace(p, value);
		if (ret == 0)//成功
		{
			*len = strlen(value);
			flag = 1;
			goto End;
		}
		else
		{
			goto End;
		}

	}

End:
	if (fp != NULL)
	{
		fclose(fp);
	}

	if (0 == flag)
	{
		ret = -3;
		printf("没有%s对应的value\n", key);
	}

	return ret;
}
int WriteCfgFile(char* file, char* key, char* value, int len)
{
	FILE* fp = NULL;
	int ret = 0;
	char lineBuf[1024] = { 0 };
	char buf[SIZE] = { 0 };
	char* p = NULL;
	int flag = 0;//0代表没找到,1代表找到

	//读写,“+”必须
	fp = fopen(file, "r+");
	if (fp == NULL)//当配置文件未创建时
	{
		//创建配置文件
		fp = fopen(file, "w+");
		if (fp == NULL)
		{
			ret = -2;
			goto End;
		}
	}

	//获取文件大小
	//光标移动到结尾
	fseek(fp, 0, SEEK_END);

	long size = ftell(fp);
	if (size >= SIZE)
	{
		ret = -3;
		printf("文件大小超过8k,不支持");
		goto End;
	}

	//光标回到开头
	rewind(fp);

	while (1)
	{
		if (fgets(lineBuf, sizeof(lineBuf), fp) == NULL)
		{
			break;
		}

		//每一行是否包含key
		if (strstr(lineBuf, key) != NULL)
		{//包含key
			flag = 1;
			sprintf(lineBuf, "%s = %s\n", key, value);

			strcat(buf, lineBuf);
		}
		else
		{
			strcat(buf, lineBuf);
		}
	}

	//程序到这,两种情况
	if (0 == flag)//没有key
	{
		//文件光标已经移动到文件结尾
		fprintf(fp, "%s = %s\n", key, value);
	}
	else//有key
	{
		//关闭文件
		if (fp != NULL)
		{
			fclose(fp);
			fp = NULL;
		}

		fp = fopen(file, "w+");
		if (fp == NULL)
		{
			ret = -4;
			goto End;
		}

		//重写内容
		fputs(buf, fp);
	}

End:
	if (fp != NULL)
	{
		fclose(fp);
	}

	return ret;
}

main.c

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

#define CFGNAME "./config.ini"



void menu()
{
	printf("======================\n");
	printf("1.读配置文件\n");
	printf("2.写配置文件\n");
	printf("3.清屏\n");
	printf("4.退出\n");
	printf("======================\n");
}

void MyRead()
{
	char key[256] = { 0 };
	char value[1024] = { 0 };
	int len = 0;
	int ret = 0;

	printf("请输入key值:");
	scanf("%s", key);

	ret = ReadCfgFile(CFGNAME, key, value, &len);
	if (ret != 0)
	{
		printf("ReadCfgFile err:%d\n", ret);
		return;
	}

	printf("\n%s对应的value:%s, len = %d\n", key, value, len);

}

void MyWrite()
{
	char key[256] = { 0 };
	char value[1024] = { 0 };
	int len = 0;
	int ret = 0;

	printf("请输入key值:");
	scanf("%s", key);

	printf("请输入value值:");
	scanf("%s", value);

	ret = WriteCfgFile(CFGNAME, key, value, strlen(value));
	if (ret != 0)
	{
		printf("WriteCfgFile err:%d\n", ret);
		return;
	}

	printf("\n写入成功,输入的是:%s对应的value:%s, len = %d\n", key, value, strlen(value));

}

int main()
{
	int cmd;

	while (1)
	{
		menu();
		printf("cmd:");
		scanf("%d", &cmd);
		switch (cmd)
		{
		case 1:
			MyRead();
			break;
		case 2:
			MyWrite();
			break;
		case 3:
			system("cls");
			break;
		case 4:
			exit(0);
		}
	}


	printf("\n");
	system("pause");
	return 0;
}
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

banjitino

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值