C简单的配置文件操作
2011-12-21 14:50
411 查看
#include "stdafx.h"
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define CFGFILE "CfgFile.conf"
/***************************************************************
*函数名:GetCfgVal
*参数:cFileName 配置文件名 key要查的键 value 查到的值
*返回值:成功返回1,并把值存到value;失败返回 0
*功能:从基本的配置文件中读取配置.
****************************************************************/
int GetCfgVal(char* cFileName,char *key,char* value)
{
FILE *fOpen;
char cBuf[512] = {0};
char tmpBuf[512];
char *p,*q;
int nRet = 0;
fOpen = fopen(cFileName,"r");
if(fOpen != NULL)
{
while(!feof(fOpen))
{
fgets(cBuf,sizeof(cBuf),fOpen);
if(cBuf[0] == '#')
continue;
strcpy(tmpBuf,cBuf);
q =p = strchr(tmpBuf,'=');
if(p > 0)
{
while(*p&&*(p-1)==' ') p--;
*p = 0;
if(strcmp(tmpBuf,key)==0)
{
nRet = 1;
q++;
while(*q && *q==' ') q++;
if(q[strlen(q)-1]=='\n')
q[strlen(q)-1]=0;
strcpy(value,q);
}
}
}
}else
{
nRet = 0;
}
fclose(fOpen);
return nRet;
}
int main(int argc, char* argv[])
{
char buf[256]={0};
char key[][256] = {"IP","PORT","SERVERNAME","HOMEDIR","USER","PASSWORD"};
int i;
printf("\n\tCfgFileParse Example.\n");
for(i=0;i<6;i++)
if(GetCfgVal(CFGFILE,key[i],buf))
printf("%s:%s\n",key[i],buf);
else
printf("Failed to get %s\n",key[i]);
return 0;
}
相关文章推荐
- Java中配置文件Properties类的简单操作
- 2. 数据库文件配置与简单操作 Model / M()
- C#操作xml配置文件最简单方法
- java 属性文件(配置文件)的简单操作
- Java简单操作Properties配置文件(实例详解)
- java从操作配置文件中连接数据库的简单写法
- c#配置文件的简单操作
- 简单的操作xml ,用于在winform中配置文件.
- RHEL5.5使用SAMBA配置简单文件共享
- 在 Eclipse Workbench 之外使用 Eclipse GUI,第 3 部分: 添加操作、菜单和工具栏完成简单的文件资源管理器应用程序
- 初识Hibernate及简单的手动创建配置文件
- 简单易懂的snmpd.conf配置文件说明
- activemq入门安装启动和配置文件简单讲解
- java配合properties配置文件对Excel表格文件的操作
- 简单sql查询接口(需要在文件中指定conn才能进行sql语句操作)
- (转)实现对Java配置文件Properties的读取、写入与更新操作
- C语言简单文件操作
- linux : gerrit搭建配置服务器+git简单操作+apache代理配置+部分遇到的错误及处理
- ansible 用变量简单渲染nginx配置文件
- C语言最简单的文件操作