我无法使用C和libconfig从配置文件中检索用户值。
configreader.h
#include <stdio.h>
#include <string.h>
#include <libconfig.h>
#ifndef CONFIGREADER_H_INCLUDED
#define CONFIGREADER_H_INCLUDED
char *userreader(){
static const char *inputfile = "/opt/plaininc/config/handler.conf";
config_t configreader;
const char *userconfig;
if(! config_read_file(&configreader, inputfile)){
config_destroy(&configreader);
exit(EXIT_FAILURE);
}
if (config_lookup_string(&configreader, "handler.user", &userconfig)){
config_destroy(&configreader);
return userconfig;
} else {
config_destroy(&configreader);
exit(EXIT_FAILURE);
}
}
#endif
CONFIG.c
#include <stdio.h>
#include <stdlib.h>
#include "configreader.h"
int main(){
const char *user = userreader();
printf("%s", user);
}
GDB:
编程接收信号SIGSEGV,分段故障。 0xb7e63496 in 来自/lib/i386-linux-gnu/libc.so.6的free()
答案 0 :(得分:2)
您需要使用config_init()
初始化configreader
。做:
config_t configreader;
const char *userconfig;
config_init(&configreader);
....