* Please refer to this document for reference from blog.csdn.net/wtz1985
G_key_file_new () and g_key_file_load_from_file () interfaces have meanings such as file * FP and fopen (), but they have special meanings, that is, when reading files, not all are read. Instead, it reads data based on its own identifiers and then separates the group, key, and key values (integer, string, and double) based on the corresponding interface functions ). Therefore, when writing a file, pay attention to the specific format. It is a bit similar to XML, but it is much simpler than XML. The following is a simple code explanation:
1. Create a tagged file:
- /* ------ Data.txt -------*/
- [One]
- Key_one = 1
- [Tow]
- Key_tow = 2
- [Three]
- Key_three = 3
2. Simple code implementation:
- /* ---- Main. c -----*/
- # Include <stdlib. h>
- # Include <stdio. h>
- # Include <glib. h>
- # Define file_name "/home/wutangzhi/wtz/test_dir/test_file/data.txt"
- Int main (INT argc, char ** argv)
- {
- Char ** groups = NULL;
- Char ** key = NULL;
- Gkeyfile * Keyfile;
- Gsize length = 0;
- Gerror * error = NULL;
- Glist * list_group = NULL;
- Keyfile = g_key_file_new ();
- G_key_file_load_from_file (Keyfile, file_name, g_key_file_none, & error );
- Groups = g_key_file_get_groups (Keyfile, & length );
- Gsize length_keys = 0;
- Gsize I = 0;
- For (I = 0; I <length; I ++)
- {
- Key = g_key_file_get_keys (Keyfile, groups [I], & length_keys, & error );
- Gsize J = 0;
- For (j = 0; j <length_keys; j ++)
- {
- Printf ("Key = % s/n", key [J]);
- Double value = g_key_file_get_double (Keyfile, groups [I], key [J], & error );
- Printf ("key_string = % F/N", value );
- }
- }
- Return 0;
- }
3. Display Results:
- Key = key_one
- Key_string = 1.000000
- Key = key_tow
- Key_string = 2.000000
- Key = key_three
- Key_string = 3.000000
This function interface can read data differently based on different attributes. It is quite useful when appropriate, such as unit conversion.