项目要求:将如下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;
}
实现效果: