项目开发写配置文件ini——20200226

本文介绍了一种在C++中实现配置文件读写的接口方法,包括读取特定键值和写入新键值的功能。通过定义接口并在主界面提供选项,用户可以轻松地读取或修改配置文件内容。

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

项目要求:将如下key查找和写入新的key

key1=value111
key2=  value2  
key3  =  value3  
key4  =
key5  =  value5555  
key6=value666

一、接口要求:Cfg_op.h

#pragma once
#ifndef _CFG_OP_H_
#define _CFG_OP_H_

#ifdef __cplusplus
extern "C" {
#endif
	int ReadCfgItem(char* pFlieName, char* pKey, char* pValue);
	int WriteCfgItem(char* pFlieName, char* pKey, char* pValue, int* pValueLen);

#ifdef __cplusplus
}
#endif

#endif

二、测试集成界面 main.cpp

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

#define CFGNAME "D:/CPP/C_20200120/项目配置文件开发3/Cfg1.ini"

void showmenu()
{
	printf("====================\n");
	printf("01-->读配置文件\n");
	printf("02-->写配置文件\n");
	printf("00-->退出程序\n");
	printf("====================\n");
}

int TReadCfg()
{
	int ret = 0;
	char keyname[64] = { 0 };
	char keyvalue[64] = { 0 };
	printf("input key name:");
	scanf("%s", keyname);
	ret = ReadCfgItem(CFGNAME, keyname, keyvalue);
	if (ret != 0)
	{
		printf("func ReadCfgItem() error:%d\n", ret);
		return ret;
	}
	printf("%s=%s\n\n", keyname, keyvalue);
	return ret;
}

int TWriteCfg()
{
	int ret = 0;
	char keyname[64] = { 0 };
	char keyvalue[64] = { 0 };
	int len = 0;
	printf("input key name:");
	scanf("%s", keyname);
	printf("input key value:");
	scanf("%s", keyvalue);
	ret = WriteCfgItem(CFGNAME, keyname, keyvalue, &len);
	if (ret != 0)
	{
		printf("func WriteCfgItem() error:%d\n", ret);
		return ret;
	}
	printf("%s=%s\n\n", keyname, keyvalue);
	return ret;
}

int main()
{
	int choose;
	while (1)
	{
		showmenu();
		printf("please input choose:");
		scanf("%d", &choose);
		switch (choose)
		{
		case 1:
			TReadCfg();
			break;
		case 2:
			TWriteCfg();
			break;
		case 0:
			exit(0);
			break;
		default:
			exit(0);
		}
	}
	printf("\n");
	system("pause");
	return 0;
}

三、接口实现代码:Cfg_op.cpp

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

#define MaxlineCount 1024

int ReadCfgItem(char* pFlieName, char* pKey, char* pValue)
{
	int ret = 0;
	char linebuf[MaxlineCount];
	FILE* fp = NULL;
	char* pTmp = NULL;
	char* pBegin = NULL;
	char* pEnd = NULL;
	int lengh = 0;
	fp = fopen(pFlieName, "r");
	if (fp == NULL)//Open failed
	{
		ret = -1;
		printf("fopen error:%d\n", ret);
		goto End;
	}
	//
	while (!feof(fp))
	{
		fgets(linebuf, MaxlineCount, fp);
		pTmp = strchr(linebuf, '=');
		if (pTmp == NULL)
		{
			continue;
		}
		pTmp = strstr(linebuf, pKey);
		if (pTmp == NULL)
		{
			continue;
		}
		pTmp = pTmp + strlen(pKey);
		pTmp = strchr(pTmp, '=');
		pTmp = pTmp + 1;
		//fine start point
		while (1)
		{
			if (*pTmp == ' ')
			{
				pTmp++;
			}
			else
			{
				pBegin = pTmp;
				if (*pBegin == '\n')
				{
					goto End;
				}
				break;
			}
		}
		//fine end point
		while (1)
		{
			if (*pTmp == ' ' || *pTmp == '\n')
			{
				break;
			}
			else
			{
				pTmp++;
			}
		}
		pEnd = pTmp;
		strncpy(pValue, pBegin, pEnd - pBegin);
	}
End:
	if (fp == NULL)
	{
		fclose(fp);
	}
	return ret;
}

int WriteCfgItem(char* pFlieName, char* pKey, char* pValue, int* pValueLen)
{
	int ret = 0;
	int iTag = 0;
	char linebuf[MaxlineCount];
	char filebuf[1024 * 8] = { 0 };
	FILE* fp = NULL;
	char* pTmp = NULL;
	int lengh = 0;
	//漏了
	if (pFlieName == NULL || pKey == NULL || pValue == NULL)
	{
		ret = -1;
		printf("WriteCfgItem() err. param err \n");
		goto End;
	}
	fp = fopen(pFlieName, "r+");//以读写方式打开
	if (fp == NULL)//Open failed
	{
		fp = fopen(pFlieName, "w+t");//New file again
		if (fp == NULL)
		{
			ret = -2;
			printf("fopen error:%d\n", ret);
			goto End;
		}
	}
	//about file
	fseek(fp, 0L, SEEK_END);//把光标移到文件结尾
	lengh = ftell(fp);//整个文件长度
	if (lengh > 1024 * 8)
	{
		ret = -2;
		printf("file not support!\n");
		goto End;
	}
	fseek(fp, 0L, SEEK_SET);//把光标移到文件开头

	//写配置文件-->
	while (!feof(fp))
	{
		//初始化
		memset(linebuf, 0, sizeof(linebuf));
		pTmp = fgets(linebuf, MaxlineCount, fp);
		if (pTmp == NULL)//文件为空
		{
			break;
		}
		//1-->先判断key是否在本行
		pTmp = strstr(linebuf, pKey);
		if (pTmp == NULL)//不在本行
		{
			//把本行数据存到buf
			strcat(filebuf, linebuf);
			continue;
		}
		else //在本行-->替换本行数据
		{
			sprintf(linebuf, "%s=%s\n", pKey, pValue);
			strcat(filebuf, linebuf);
			//存在key
			iTag = 1;
		}
	}
	//若关键字不存在,则需追加-->add
	if (iTag == 0)//New key
	{
		fprintf(fp, "%s=%s\n", pKey, pValue);
	}
	else //存在则将文件缓存数据拷贝到fp中
	{
		if (fp != NULL)
		{
			fclose(fp);
			fp = NULL; //避免野指针
		}
		fp = fopen(pFlieName, "w+t");
		if (fp == NULL)
		{
			ret = -4;
			printf("fopen() err. \n");
			goto End;
		}
		fputs(filebuf, fp);
	}
End:
	if (fp != NULL)
	{
		fclose(fp);
	}
	return ret;
}

 实现效果:


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值