我有一个声明为extern conf_t conf
.
typedef struct {
int home_dir_len;
char *home_dir;
int key_file_len;
char *key_file;
unsigned int max_mem;
unsigned int runtime;
} conf_t;
我试图通过下面的函数设置它的变量,但是只设置整数值,而不是字符串。
if (strcmp(tok1, "HOME_DIR") == 0) {
char *dir = strtok(NULL, &delim);
conf.home_dir_len = strlen(dir);
conf.home_dir = dir;
}
else if (strcmp(tok1, "KEY_FILE") ==0) {
char *key = strtok(NULL, &delim);
conf.key_file_len = strlen(key);
conf.key_file = calloc(conf.key_file_len +1, sizeof(char));
conf.key_file = key;
}
else if (strcmp(tok1, "MAX_MEM") ==0) {
conf.max_mem = atoi(strtok(NULL, &delim));
}
else if (strcmp(tok1, "RUNTIME") ==0) {
conf.runtime = atoi(strtok(NULL, &delim));
}
else {
perror("you shouldnt be here");
}
这是输出:
conf.home_dir_len = 5 conf.home_dir = ' and more empty lines ' **This should be /tmp/** conf.key_file_len = 10 conf.key_file = 'nd more empty lines ' **this should be myfile.key** conf.max_mem = 10 conf.runtime = 10
你能解释一下为什么以及如何纠正它吗?