读取到配置文件的C语言的接口实现
来源:互联网 发布:淘宝基础版店铺装修 编辑:程序博客网 时间:2024/05/21 06:36
/********************************************************************* * Author : Samson * Date : 03/13/2015 * Test platform: * 3.13.0-24-generic * GNU bash, 4.3.11(1)-release * *******************************************************************/
为了完成读取系统中的配置文件的某个key键的值,由于使用别人的库总是不爽,而且对于格式有一定的要求,那么就自己来写一个这样的接口以供使用了。实现原理很简单,通过打开配置文件,进行一行一行的读取,对比行中是否存在key串且此key串的下一个字符是否为'=',若是,则得到'='号之后的值。
注意:此实现方法只适用于key=value这样的配置格式,而不是很多配置文件的key="value"的格式。以下即是此接口的实现及测试代码:
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int get_conf_value(char *file_path, char *key_name, char *value)
{
FILE *fp = NULL;
char *line = NULL, *substr = NULL;
size_t len = 0, tlen = 0;
ssize_t read = 0;
if(file_path == NULL || key_name == NULL || value == NULL)
{
printf("paramer is invaild!\n");
return -1;
}
fp = fopen(file_path, "r");
if (fp == NULL)
{
printf("open config file is error!\n");
return -1;
}
while ((read = getline(&line, &len, fp)) != -1)
{
substr = strstr(line, key_name);
if(substr == NULL)
{
continue;
}
else
{
tlen = strlen(key_name);
if(line[tlen] == '=')
{
strncpy(value, &line[tlen+1], len-tlen+1);
printf("config file format is invaild tlen is %d len is %d\n", tlen, len);
tlen = strlen(value);
printf("get value is %s tlen is %d\n", value, tlen);
//replace enter key
*(value+tlen-1) = '\0';
break;
}
else
{
printf("config file format is invaild tlen is %d len is %d\n", tlen, len);
fclose(fp);
return -2;
}
}
}
if(substr == NULL)
{
printf("key: %s is not in config file!\n", key_name);
fclose(fp);
return -1;
}
free(line);
fclose(fp);
return 0;
}
int main()
{
char getva[128] = {0};
char pathname_key[] = "Path";
char profilename[] = "/home/ufo/.mozilla/firefox/profiles.ini";
int ret = get_conf_value(profilename, pathname_key, getva);
if(ret == 0)
printf("get pathname_key's value from profile:%s is %s\n", profilename, getva);
return ret;
}
其中profilename是firefox的配置文件,获取key:Path的值。运行结果如下:
ufo@ufo:~/$ ./a.out
config file format is invaild tlen is 4 len is 120
get value is cojs83dh.default
tlen is 17
get pathname_key's value from profile:/home/ufo/.mozilla/firefox/profiles.ini is cojs83dh.default
配置文件的内容如下:
ufo@ufo:~$ cat /home/ufo/.mozilla/firefox/profiles.ini
[General]
StartWithLastProfile=1
[Profile0]
Name=default
IsRelative=1
Path=cojs83dh.default
Default=1
405
0 0
- 读取到配置文件的C语言的接口实现
- 读取到配置文件的C语言的接口实现
- 网上找的c语言读取配置文件
- C语言读取配置文件的另类写法
- C语言实现的配置文件读取功能,喜欢的拿去用.
- Linux C语言实现读取ini配置文件
- 用c语言实现读取配置文件源码
- c语言实现配置文件的读写
- C语言读取配置文件
- C语言读取配置文件
- C语言读取配置文件
- C语言读取配置文件
- c/c++: 读取配置文件(文本)内容的简单实现
- 读取配置文件csv的接口封装类
- Linux环境用C语言实现读取配置文件来区分产品的多个版本以及多个功能
- 读取配置文件[方式一]之纯C语言实现
- window下C语言实现读取config.ini配置文件
- C语言读取配置文件例子
- Laravel5.0学习之——搭建环境
- UUID
- 查询sql
- 使用Search查询Office 365 Groups
- 关于laravel错误解决方案
- 读取到配置文件的C语言的接口实现
- xamarin .forms 手机的一些信息获取Device Info Plugin
- ConcurrentHashMap源码解读
- Leetcode: Count and Say
- 可以返回执行结果的system函数加强版本
- hdu 5179
- unity3D中Socket链接与服务之间的通讯测试程序
- 组播,单播,广播,多播,泛洪的基本概念
- 程序员的自我修养