我使用 libconfig C api 来解析属性文件。我在前两个属性中得到了空值。
测试属性
x = "hello";
y = "world";
z = "test" ;
配置文件
char * getValueByKey(char * file_name , char * key) {
config_t cfg;
config_setting_t *setting;
const char * value;
config_init(&cfg);
if (!config_read_file(&cfg, file_name))
{
printf("\n%s:%d - %s", config_error_file(&cfg), config_error_line(&cfg), config_error_text(&cfg));
config_destroy(&cfg);
exit(1);
}
if (config_lookup_string(&cfg, key , &value)){
config_destroy(&cfg);
printf("Hello %s\n",value );
return (char *) value ;
}
else{
printf("\nNo 'filename' setting in configuration file.");
config_destroy(&cfg);
}
}
int main(){
char * x = getValueByKey("test.properties" , "x");
char * y = getValueByKey("test.properties" , "y");
char * z = getValueByKey("test.properties" , "z");
printf("Values of X : Y : Z = %s : %s : %s", x, y, z);
}
运行我的程序后,我只得到 Z 值。
输出:
Values of X : Y : Z = : : test
我尝试了很多样本我的前两个属性值为空;