I try to write a program using this structure containing strings :
typedef struct s_conf
{
char *shell1;
char *shell2;
char *shell3;
char *shell4;
char *server_ip;
} t_conf;
Parsing a config text file line per line, I get this information and I store it into variables such as line1 and line4. Now I want to assign my struct fields the values of the variables line1 and line4:
char *line1 = "/var/www/host/current/app/console robot:file";
char *line4 = "192.168.00.00";
t_conf *conf;
if ((conf = malloc(sizeof(t_conf))) == NULL)
{
fprintf(stderr, "Malloc error\n");
return (-1);
}
strcpy(conf->shell1, line1);
strcpy(conf->server_ip, line4);
printf("line1 : '%s'\n"; line1);
printf("line4 : '%s'\n"; line4);
printf("t_conf->shell1 : '%s'\n", conf->shell1);
printf("t_conf->server_ip : '%s'\n", conf->server_ip);
The output :
line1 : '/var/www/host/current/app/console robot:file'
line4 : '192.168.00.00'
t_conf->shell1 : '/var/www/host/current/app'
t_conf->server_ip : '192.168.00.00'
How to correctly assign the c string t_conf->shell1 ?
I try other functions like memcpy(), strdup() and allocate the variable with malloc : t_conf->shell1 = malloc(strlen(line1) + 1) but it gives me the same result, I lose a portion of line1 ?