1.生成一个文件test.ini:建立GenerateIniFIle.c
#include
main()
{
gnome_config_set_string("=test.ini=test=t1", "sunligang");
gnome_config_set_string("=test.ini=test=t2", "2");
gnome_config_set_string("=test.ini=test=t3", "3");
gnome_config_sync();
}
gcc GenerateIniFIle.c `pkg-config --cflags --libs libgnome-2.0`
sudo ./a.out
打开test.ini:
[test]
t1=sunligang
t2=2
t3=3
2.新建一个文件test.c
#include
#include
main()
{
int *handle;
handle = gnome_config_init_iterator("=test.ini=test"); // 算是打开SECTION吧
char *key, *value;
while (handle) // 如果有值
{
/* 读取下一个Ident */
handle = gnome_config_iterator_next(handle, &key, &value);
if (! key) // 不为NULL
break;
printf("%s,%s\n", key, value); // 打出来看看
}
}
3.编译: gcc test.c `pkg-config --cflags --libs libgnome-2.0`
4.运行:sudo ./a.out
5.结果:
t3,3
t2,3
t1,sunligang
说明:
一 、写INI文件
/* 第一个参数为的test.ini为文件名称,www为section,qwe为ident
第二个参数为值
它们之间用=分割,则会在程序当前目录生成INI文件
据说用/分割会在 ~/.gnome或~/.gnome_private目录下生成INI文件
不知道为什么,我改成/后运行出错,我的系统为FC6
*/
gnome_config_set_string("=test.ini=www=qwe", "test123");
/* 必须调用下面这句,否则将些不成功 */
gnome_config_sync();
二、读INI文件
char *str = gnome_config_get_string("=test.ini=www=qwe");
// 另一种读法
// 后面多出来的为默认值,
// 通过执行gnome_config_get_string_with_default函数
// 如果在INI文件中读出qwe的值,则o为0否则为1
int o;
char *str = gnome_config_get_string_with_default("=test.ini=www=qwe=oo", &o);
printf("%s;%d", str, o);
记得要
#include
编译,因为对linux不熟悉,编译它让我费了很大的精力
gcc 文件.c `pkg-config --cflags --libs libgnome-2.0`
三、例子:读取某一个SECTION下面的所有节(Ident)
例如:
[test]
t1=1
t2=3
t3=3
如何在不知道t1"t2"t3的情况下,读出它们呢?
int *handle;
handle = gnome_config_init_iterator("=test.ini=test"); // 算是打开SECTION吧
char *key, *value;
while (handle) // 如果有值
{
/* 读取下一个Ident */
handle = gnome_config_iterator_next(handle, &key, &value);
if (! key) // 不为NULL
break;
printf("%s;%s"r"n", key, value); // 打出来看看
}
需要注意的是,这么读出来的顺序不是t1,t2,t3而是t3,t2,t1。
没有评论:
发表评论