cfg.h
#ifndef _CFG_OP_H_
#define _CFG_OP_H_
#ifdef __cplusplus
extern "C" {
#endif
//写配置文件
int WriteCfgItem(char *PFilename/*in*/,char *PKey/*in*/,char *PValue/*in*/,char *PValuelen/*in*/);
//读配置文件
int GetCfgItem(char *filename/*in*/,char*key/*in*/,char *value/*in*/,int *PValuelen/*out*/);
//去除空格函数
int trimSpace(char *instr,char *outstr);
int trimspace(char *instr,char *outstr);
#ifdef __cplusplus
}
#endif
#endif
cfg.c
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAXLINE 2048
//写配置文件
int WriteCfgItem(char *PFilename/*in*/,char *PKey/*in*/,char *PValue/*in*/,char *PValuelen/*in*/)
{
int ret = 0;
FILE* fp;
int length = 0;
char *temp = NULL;
char linebuf[MAXLINE];
char filebuf[1024*8]={0}; //文件缓冲区
int iTag = 0;
//判断传参是否正确
if( PFilename==NULL || PKey==NULL || PValue==NULL )
{
ret = -1;
printf("func WriteCfgItem() 参数 error %d.\n",ret);
return ret;
}
//以追加的方式打开文件
fp = fopen(PFilename,"a+");
//判断是否打开错误
if( fp == NULL )
{
ret = -2;
printf("func WriteCfgItem() open error %d.\n",ret);
return ret;
}
//存储文件长度
fseek(fp,0L,SEEK_END); //文件指针定位到文件末尾
length = ftell(fp); //返回长度
fseek(fp,0L,SEEK_SET); //文件指针定位到文件开头
//判断文件是否大于8K
if( length > 1024*8 )
{
ret = -3;
printf("func WriteCfgItem() length error %d.\n",ret);
return ret;
}
//扫描全文件,判断key是否已存在,无则追加
while(!feof(fp))
{
memset(linebuf,0,MAXLINE);
temp = fgets(linebuf,MAXLINE,fp);
//判空跳出
if( temp == NULL )
{
break;
}
//查找关键字
temp = strstr(linebuf,PKey);
if( temp != NULL )
{
//关键字在本行
//覆盖本行文件
sprintf(linebuf,"%s = %s\n",PKey,PValue);
strcat(filebuf,linebuf);
iTag = 1;
}
else
{
//关键字不在本行
strcat(filebuf,linebuf);//追加本行到filebuf中
continue;//继续下轮循环
}
}
//文件最终处理
if(iTag == 0)//追加
{
ret = fprintf(fp,"%s = %s\n",PKey,PValue);
if( ret == 0 )
{
ret = -4;
printf("func WriteCfgItem() write error %d.\n",ret);
return ret;
}
else
{
fclose(fp);
}
}
else
{//覆盖旧文件
if( fp != NULL )
{
fclose(fp);
}
fp = fopen(PFilename,"w+t");
if( fp == NULL )
{
ret = -5;
printf("func WriteCfgItem() open error %d.\n",ret);
return ret;
}
ret = fputs(filebuf,fp);
if ( ret < 0 )
{
printf("func fputs err \n");
return ret;
}
if (fp!=NULL)
{
fclose(fp);
}
}
return 0;
}
//读配置文件
int GetCfgItem(char *filename/*in*/,char *key/*in*/,char *value/*in*/,int *PValuelen/*out*/)
{
int ret = 0;
FILE* fp = NULL;
char linebuf[MAXLINE];
char *ptemp = NULL;
//以只读方式打开文件
fp = fopen(filename,"r");
//判断文件是否打开成功
if(fp == NULL)
{
ret = -1;
printf("func open() error %d.\n",ret);
}
//判断是否读完文件
while(!feof(fp))
{
//清空存储区
memset(linebuf,0,MAXLINE);
//整行读取文件
fgets(linebuf,MAXLINE,fp);
//printf("linebuf = %s\n",linebuf);
//判断是否存在等号
ptemp = strchr(linebuf,'=');
if(ptemp == NULL)
{
continue;
}
//判断并进行进行挖串操作
ptemp = strstr(linebuf,key);
if(ptemp == NULL)
{
continue;
}
ptemp = ptemp + strlen(key);
ptemp = strchr(linebuf,'=');
if(ptemp == NULL)
{
continue;
}
ptemp = ptemp + 1;
ret = trimspace(ptemp,value);
if(ret != 0)
{
ret = -2;
printf("func trimspace error %d.\n",ret);
return ret;
}
}
if(fp != NULL)
fclose(fp);
return ret;
}
//挖串
int trimspace(char *instr,char *outstr)
{
int ret = 0;
char *p = NULL;
p = instr;
if( p == NULL)
{
ret = -1;
printf("func trimspace error %d.\n",ret);
return ret;
}
while(*(p++) != '\0')
{
if(*p != ' ')
{
*outstr++ = *p;
}
}
*outstr = '\0';
return ret;
}
//挖串
int trimSpace(char*inStr,char*out)
{
char *p = inStr;
int ret = 0;
int i;
if (p==NULL)
{
ret = -1;
return;
}
for (i=0;p[i]!='\0';i++)
{
if (p[i]!=' ')
{
*out++ = p[i];
}
}
*out='\0';
return ret;
}
test_cfg.c
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "cfg_op.h"
#define CFGNAME "d:/cfg.ini"
void menu_display()
{
printf("------------------------------\n");
printf("1.读文件测试\n");
printf("2.写文件测试\n");
printf("0.退出\n");
printf("------------------------------\n");
printf("请输入选项:");
}
//写配置文件
int WriteCfg()
{
char key[1024] = {0};
char value[1024] = {0};
int ret = 0;
printf("\n请键入key:");
scanf("%s",key);
printf("\n请键入value:");
scanf("%s",value);
ret = WriteCfgItem(CFGNAME/*in*/,key/*in*/,value/*in*/,strlen(value)/*in*/);
if(ret != 0)
{
printf("func WriteCfg() WriteCfgItem error %d!\n",ret);
return ret;
}
printf("你的输入是:%s = %s\n",key,value);
return 0;
}
//读配置文件
int GetCfg()
{
//GetCfgItem(char *filename/*in*/,char*key/*in*/,char *value/*in*/,char *PValuelen/*out*/);
char key[1024] = {0};
char value[1024] = {0};
int vlen = 0;
int ret = 0;
printf("\n请键入key:");
scanf("%s",key);
ret = GetCfgItem(CFGNAME/*in*/,key/*in*/,value/*in*/,&vlen/*in*/);
if(ret != 0)
{
printf("func GetCfgItem() error %d!\n",ret);
return ret;
}
printf("value:%s\n",value);
return 0;
}
void main()
{
int choice;
for(;;)
{
//显示菜单
menu_display();
//输入选项
scanf("%d",&choice);
switch(choice)
{
case 1:GetCfg();
break;
case 2:WriteCfg();
break;
case 0:
exit(0);
default:
exit(0);
}
}
system("pause");
}