博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
glib-读取配置文件
阅读量:4170 次
发布时间:2019-05-26

本文共 2844 字,大约阅读时间需要 9 分钟。

1. 配置文件

配置文件的格式如下:

[组名]#注释Key = value#注释Key = value#注释Key = value#注释Key = value#注释Key = value#注释Key = value
 

2.glib读写配置文件

glib中的Key-value file parser 可以读写配置文件,下面具体介绍以下怎么使用:

加载配置文件:

static GKeyFile*configure_open_file (const gchar *filename){        GError *err = NULL;        GKeyFile *keyfile;        keyfile = g_key_file_new ();        g_key_file_set_list_separator (keyfile, ',');        if (!g_key_file_load_from_file (keyfile, filename, G_KEY_FILE_NONE, &err)){                g_error_free (err);                g_key_file_free (keyfile);                return NULL;        }        return keyfile;}
 

该函数用来加载配置文件并返回GKeyFile指针,如果出错则返回NULL。

函数:

gboolean g_key_file_load_from_file (GKeyFile *key_file,

                                                                                     const gchar *file,

                                                                                     GKeyFileFlags flags,

                                                                                     GError **error);

参数 GKeyFileFlags falgs:

G_KEY_FILE_NONE

No flags, default behaviour

G_KEY_FILE_KEEP_COMMENTS

Use this flag if you plan to write the (possibly modified) contents of the key file back to a file; otherwise all comments will be lost when the key file is written back.

G_KEY_FILE_KEEP_TRANSLATIONS

Use this flag if you plan to write the (possibly modified) contents of the key file back to a file; otherwise only the translations for the current language will be written back.

 

从配置文件中读取整数:

 

static gintconfigure_read_int (GKeyFile *keyfile, gchar *sec, gchar *key, gint default_val){        GError *err = NULL;        int val = g_key_file_get_integer (keyfile,                                          sec,                                          key,                                          &err);        if (err){                error (err->message);                g_error_free (err);                return default_val;        }        return val;}
 

 

从配置文件中读取字符串:

static voidconfigure_read_string (GKeyFile *keyfile, GString *ret, gchar *sec, gchar *key, gchar *default_val){        GError *err = NULL;        gchar* val = g_key_file_get_string (keyfile,                                          sec,                                          key,                                          &err);        if (err){                g_string_assign (ret, default_val);                g_error_free (err);                return;        }        g_string_assign (ret, val);        g_free (val);}

 读取的字符串放在了ret中, 同时上面两个函数都提供可默认值的功能,即读取失败时设置默认值。

 

3.glib读写配置文件的例子

#include 
void main (){ GKeyFile *keyfile = oj_configure_open_file ("test.conf"); GString *test = g_string_new (""); oj_configure_read_string (keyfile, conf->oj_host_name, "TEST", "test1", "test1"); gint int_test = oj_configure_read_int (keyfile, "TEST", "test_int", 0); g_string_free (test, TRUE); g_key_file_free (keyfile);}

 

编译命令:

gcc -o test test `pkg-config --cflags --libs glib-2.0`
 

 

 

转载地址:http://hzbai.baihongyu.com/

你可能感兴趣的文章
最全BAT算法面试130题:阿里、百度、腾讯、京东、美团、今日头条
查看>>
想进阿里P7,你必须掌握这些技能专题
查看>>
看完你还敢说你懂JVM吗?
查看>>
面试了一个2年程序员,竟然只会curd,网友神回复!
查看>>
ElasticSearch基础分布式架构讲解
查看>>
年底了,程序员来说说你今年写过的最牛逼的bug是什么?
查看>>
阿里P8架构师讲述:3—5年程序员的发展和出路在哪里?
查看>>
题库分库分表架构方案
查看>>
一篇文读懂缓存在大型分布式系统中的最佳应用
查看>>
当亲戚问你工资,程序猿如何作答,简直不能再机智
查看>>
Redis从单机到集群,一步步教你环境部署以及使用
查看>>
电商平台备战促销季的运维秘诀——高可用服务层
查看>>
从零开始实现RPC框架 - RPC原理及实现
查看>>
MySQL索引优化分析
查看>>
RabbitMQ分布式集群架构
查看>>
MySQL每秒57万的写入,带你飞~
查看>>
Java系统高并发的解决方案
查看>>
学习分布式系统需要怎样的知识?
查看>>
一网打尽消息队列在大型分布式系统中的实战精髓
查看>>
阿里巴巴系统架构首次曝光
查看>>