配置文件读写系统方法和测试函数
- #include"readConfig.h"
- #define CFGNAME "./test.txt"
- void mymenu()
- {
- printf("=========================\n");
- printf("1 Test Write Config file\n");
- printf("2 Test Read Config file\n");
- printf("0 exit\n");
- printf("=========================\n");
- }
- int TGetCfg()
- {
- int ret = 0;
- char name[1024] = {0};
- char value[1024] = {0};
- int vlen = 0;
- printf("\nplease input key:");
- scanf("%s,",name);
- ret = GetCfgItem(CFGNAME, name, value, &vlen);
- if (0 != ret)
- {
- printf("func WriteCfgItem err:%d \n",ret);
- return ret;
- }
- printf("value:%s \n",value);
- return ret;
- }
- int TWriteCfg()
- {
- int ret = 0;
- char name[1024] = {0};
- char value[1024] = {0};
- printf("\nplease input key:");
- scanf("%s",name);
- printf("\nplease input value:");
- scanf("%s", value);
- ret = WriteCfgItem(CFGNAME, name, value, strlen(value));
- if (0 != ret)
- {
- printf("func WriteCfgItem err : %d \n", ret);
- return ret;
- }
- printf("your input name is %s , value is %s\n", name, value);
- return ret;
- }
- int main()
- {
- int choice;
- for(;;)
- {
- mymenu();
- scanf("%d",&choice);
- switch(choice)
- {
- case 1:
- TWriteCfg();
- break;
- case 2:
- TGetCfg();
- break;
- case 0:
- exit(0);
- default:
- exit(0);
- }
- }
- printf("end......");
- return 0;
- }
- /*
- readConfig.h
- */
- #ifndef _READCONFIG_H__
- #define _READCONFIG_H__
- #include<stdlib.h>
- #include<string.h>
- #include<stdio.h>
- #define MaxLine 2048
- #ifdef __cplusplus
- extern "C"{
- #endif
- int GetCfgItem(char *pFileName, char *pKey, char *pValue, int *pValueLen);
- // 函数声明只是声明传入参数类型, 参数名称可以和实现时不一样
- int WriteCfgItem(char *pFileName, char *pItemName, char *pItemValue, int itemValueLen);
- #ifdef __cplusplus
- }
- #endif
- #endif
- /*
- readConfig.c
- */
- #include "readConfig.h"
- // 获取配置项
- int GetCfgItem(char *pFileName, char *pKey, char *pValue, int *pValueLen)
- {
- int ret = 0;
- FILE *fp = NULL;
- char *pTmp = NULL, *pEnd = NULL, *pBegin = NULL;
- char lineBuf[MaxLine];
- fp = fopen(pFileName, "r");
- if (fp == NULL)
- {
- //-1 means the file is not exist
- ret = -1;
- return ret;
- }
- while (!feof(fp))
- {
- memset(lineBuf, 0 ,sizeof(lineBuf));
- fgets(lineBuf, MaxLine, fp);
- pTmp = strchr(lineBuf, '='); // 没有 = 号行跳过
- if (pTmp == NULL)
- {
- continue;
- }
- pTmp = strstr(lineBuf, pKey); // 判断该行是否有 pKey
- if (pTmp == NULL)
- {
- continue;
- }
- pTmp = pTmp + strlen(pKey); // wang = qing ==> "= qing"
- pTmp = strchr(pTmp, '='); // 判断 pKey 后面是否有 =
- if (pTmp == NULL)
- {
- continue;
- }
- pTmp = pTmp + 1; // 有等号把指针移到 = 号后一位
- // 获取 value 的起点 去除空格, 遇到第一个非空格符号位置赋予 pBegin
- while(1)
- {
- if (*pTmp == ' ')
- {
- pTmp++;
- }
- else
- {
- pBegin = pTmp;
- if (*pBegin == '\n')
- {
- //-2 means the value is empty
- fclose(fp);
- ret = -2;
- return ret;
- }
- break;
- }
- }
- // 获取 value 终点
- // 遇到换行符或空格符终止 (value 值中不能有换行符空格符)
- while(1)
- {
- if ((*pTmp == '') || (*pTmp =='\n'))
- {
- break;
- }
- else
- {
- pTmp++;
- }
- }
- pEnd = pTmp;
- *pValueLen = pEnd - pBegin;
- memcpy(pValue, pBegin, pEnd-pBegin);
- }
- if (fp != NULL)
- {
- fclose(fp);
- }
- return 0;
- }
- /*
- 写配置文件
- 循环写每一行, 检查 key 配置项是否存在, 若存在, 则修改对应 value 值
- 若不存在, 则在文件末尾添加 "key = value"
- */
- int WriteCfgItem(char *pFileName, char *pKey, char *pValue, int ValueLen)
- {
- int rv = 0, iTag = 0, length = 0;
- FILE *fp = NULL;
- char lineBuf[MaxLine];
- char *pTmp = NULL, *pBegin = NULL, *pEnd = NULL;
- char filebuf[1024*8] = {0};
- if (pFileName == NULL || pKey == NULL || pValue == NULL)
- {
- rv = -1;
- printf("setCfgItem() error. param error \n");
- return rv;
- }
- fp = fopen(pFileName, "r+");
- if (fp == NULL)
- {
- printf("The file %s is not exist, now create it.\n", pFileName);
- fp = fopen(pFileName, "w+t");
- if (fp == NULL)
- {
- rv = -2;
- printf("create new file %s error. \n",pFileName);
- return rv;
- }
- }
- fseek(fp, 0L, SEEK_END); // 把文件指针从文件开头移动到文件结尾
- length = ftell(fp); // 获取文件长度
- if (length> 1024 *8)
- {
- rv = -3;
- printf("the size of file is too big, and no support\n");
- fclose(fp);
- return rv;
- }
- fseek(fp, 0L, SEEK_SET);
- while (!feof(fp))
- {
- memset(lineBuf, 0, sizeof(lineBuf));
- pTmp = fgets(lineBuf, MaxLine, fp);
- if (pTmp == NULL)
- {
- break;
- }
- /* 检查 pKey 是否在本行
- 不在 copy 到 filebuf 中
- 在先修改, copy 到 filebuf 中 (替换)
- 读完整个文件
- */
- pTmp = strstr(lineBuf, pKey);
- if (pTmp == NULL)
- {
- strcat(filebuf, lineBuf);
- continue;
- }
- else
- {
- sprintf(lineBuf,"%s = %s\n", pKey, pValue);
- // 将 linebuf 内容追加复制到 filebuf 中
- strcat(filebuf, lineBuf);
- iTag = 1;
- }
- }
- // 关键字不存在, 追加
- if (iTag == 0)
- {
- fprintf(fp, "%s = %s\n", pKey, pValue);
- }
- else // 若关键字存在, 则重新创建文件 先关闭源文件, 再重新从头写入该文件中
- {
- if (fp != NULL)
- {
- fclose(fp);
- fp = NULL;
- }
- fp = fopen(pFileName, "w+t");
- if (fp == NULL)
- {
- rv = -4;
- printf("fopen() err. \n");
- if (fp != NULL)
- {
- fclose(fp);
- return rv;
- }
- }
- fputs(filebuf, fp);
- }
- if (fp != NULL)
- {
- fclose(fp);
- }
- return rv;
- }
来源: http://www.bubuko.com/infodetail-3301256.html