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 gint
configure_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 void
configure_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 <glib.h>
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`
分享到:
相关推荐
Oracle 9i的安装包glib-devel-1.2.10-15.i386.rpm
glib-2.0.dl
glib-2.58.0-win64-vs2017-v141-sdk10.0.17134.12 csdn-tags: glib-2.58.0 x64 vs2017v141 sdk17134 glib-2.58.0及其依赖包: bzip2-1.0.6-win64-vc141.zip glib-2.58.0-win64-vc141.zip libiconv-1.15-win64-vc...
linux glib-2.16.5 glib-2.16.5
001-glib-gdate-suppress-string-format-literal-warning.patch 001-glib-gdate-suppress-string-format-literal-warning.patch 001-glib-gdate-suppress-string-format-literal-warning.patch
glib-2.40.0.tar.xz
glib-dev-2.12.11.zip glib-dev-2.12.11.zip glib-dev-2.12.11.zip
glib-2.56源代码。
glib-2.63.3源码包,需要的可以下载编译,可以使用。 glib库是Linux平台下最常用的C语言函数库,它具有很好的可移植性和实用性。glib是Gtk +库和Gnome的基础。glib可以在多个平台下使用,比如Linux、Unix、Windows...
glib-2.28.0.tar.gz
glib-dev.zip包含头文件,和lib库。
glib-2.12.13,编译qemu时需要
glib-2.58.1
redhat 64位安装 gcc glib-devel-1.2.10-20.el5.x86_64.rpm 需要依赖的包之一
dbus-glib-devel-0.86-5.el6.i686.rpm
dbus-glib-0.86-5.el6.i686.rpm
glib-2.56.3.tar.xz
glib-2.30.2源码, C语言进阶很好的资料
先了解一下GLib-2.0库。 GLib库是用C语言实现的一个通用的、可移植的实用程序库,由GNOME的GTK小组开发并维护,最早用在GTK+程序开发上,后来被越来越多的程序使用。 GLib由三大部分组成——GLib、GObject和GIO,...
glib-2.32.4.tar.xz linux