-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.c
45 lines (36 loc) · 1.16 KB
/
config.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <glib.h>
int main() {
GHashTable *config = g_hash_table_new(g_str_hash, g_str_equal);
FILE *file = fopen("config.txt", "r");
if (file == NULL) {
printf("Error opening file.\n");
return 1;
}
char line[100];
while (fgets(line, sizeof(line), file)) {
if (line[0] == '#') {
continue; // 忽略注释行
}
char key[50], value[50];
sscanf(line, "%[^=]=%[^\n]", key, value);
g_hash_table_insert(config, g_strdup(key), g_strdup(value));
}
fclose(file);
// 获取配置项的值
const gchar *name = g_hash_table_lookup(config, "Name");
const gchar *age_str = g_hash_table_lookup(config, "Age");
int age = atoi(age_str);
const gchar *city = g_hash_table_lookup(config, "City");
// 输出配置项的值
printf("Name: %s\n", name);
printf("Age: %d\n", age);
printf("City: %s\n", city);
// 清理内存
g_hash_table_destroy(config);
return 0;
}
// 安装glib库: sudo apt install libglib2.0-dev
// 编译: gcc -o main config.c `pkg-config --cflags --libs glib-2.0`