我有以下 Config.cfg
[Power]
Power8=8
Temp=5=1001
Hum=7=1002
Link=8=1003
Vol=9=1004
[Power]
Power10=10
Temp=5=1012
Hum=7=1013
Link=8=1014
Vol=9=1015
并且使用 glib 我想读取每个 Power 的值。我想要类似'if Power8=8 then return temp, hum, Link, Vol'else for Power10=10
我写了这个函数
int read_config()
{
GKeyFile *keyfile;
GKeyFileFlags flags;
GError *error = NULL;
gsize length;
gchar *temperatura, *humedad, *link, *voltage;
// Create a new GKeyFile object and a bitwise list of flags.
keyfile = g_key_file_new ();
flags = G_KEY_FILE_KEEP_COMMENTS | G_KEY_FILE_KEEP_TRANSLATIONS;
// Load the GKeyFile from keyfile.conf or return.
if (!g_key_file_load_from_file (keyfile, "/home/pi/Desktop/Config.cfg", flags, &error))
{
g_error (error->message);
return -1;
}
printf("[Power]\n");
if(g_key_file_get_integer(keyfile,"Power","Power8",NULL) == 8)
{
temperatura=g_key_file_get_string(keyfile,"Power","Temp",NULL);
humedad=g_key_file_get_string(keyfile,"Power","Hum",NULL);
link=g_key_file_get_string(keyfile,"Power","Link",NULL);
voltage=g_key_file_get_string(keyfile,"Power","Volt",NULL);
printf("Power8:%d\n",g_key_file_get_integer(keyfile,"Power","Power8",NULL));
printf("Temp:%s\n",temperatura);
printf("Hum:%s\n",humedad);
printf("Link:%s\n",link);
printf("Vol:%s\n",voltage);
}
else
{
temperatura=g_key_file_get_string(keyfile,"Power","Temp",NULL);
humedad=g_key_file_get_string(keyfile,"Power","Hum",NULL);
link=g_key_file_get_string(keyfile,"Power","Link",NULL);
voltage=g_key_file_get_string(keyfile,"Power","Volt",NULL);
printf("Power10:%d\n",g_key_file_get_integer(keyfile,"Power","Power10",NULL));
printf("Temp:%s\n",temperatura);
printf("Hum:%s\n",humedad);
printf("Link:%s\n",link);
printf("Vol:%s\n",voltage);
}
}
但它返回给我 Power10 的值以及 Vol=(Null)
[Power]
Power8=8
Temp=5=1012
Hum=7=1013
Link=8=1014
Vol=(Null)
这里有什么问题?