2

プロパティファイルの解析にlibconfig C APIを使用しています。最初の 2 つのプロパティで null 値を取得しました。

test.properties

 x = "hello";
 y = "world";
 z = "test" ;

config.c

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 

多くのサンプルを試してみましたが、最初の 2 つのプロパティ値は null です。

4

1 に答える 1