http://fred-zone.blogspot.tw/2008/06/glib-gkeyfile.html

 

#include <stdio.h>
#include <glib.h>

int  main(void)
{
    GKeyFile *keyfile;
    GKeyFileFlags flags;
    GError *error = NULL;

    /* ini GKeyFile */
    keyfile = g_key_file_new();
    flags = (GKeyFileFlags) (G_KEY_FILE_KEEP_COMMENTS | G_KEY_FILE_KEEP_TRANSLATIONS) ;

    /* read config file */
    if (!g_key_file_load_from_file (keyfile, "helloworld.conf", flags, &error))
        return 0 ;

    /* read contents and print */
    printf("[Group1]\n");
    printf("mykey1: %d\n", g_key_file_get_integer(keyfile, "group1", "mykey1", NULL));
    printf("mykey2: %s\n", g_key_file_get_string(keyfile, "group1", "mykey2", NULL));

    printf("[Group2]\n");
    if (g_key_file_get_boolean(keyfile, "group2", "yourkey1", NULL))
        printf("yourkey1:  true\n");
    else
        printf("yourkey1:  true\n");

    printf("yourkey2: %lf\n",g_key_file_get_double(keyfile, "group2", "yourkey2", NULL));
}

 

g++  -I/usr/include/glib-2.0 -I/usr/lib64/glib-2.0/include test1.cpp  -lglib-2.0 -o test1.exe

pkg-config --cflags glib-2.0

pkg-config --libs glib-2.0

 

[group1]
mykey1=66
mykey2=Hello World
[group2]
yourkey1=true
yourkey2=3.141593

 

Output :

[Group1]
mykey1: 66
mykey2: Hello World
[Group2]
yourkey1:  true
yourkey2: 3.141593

 

創作者介紹
創作者 hedgezzz 的頭像
hedgezzz

hedgezzz的部落格

hedgezzz 發表在 痞客邦 留言(0) 人氣()