So i am trying to read lines and then split them in two with strtok . So if i would read "nice dog" it will first print what i read and then will print using the strtok commands "nice" and "dog" on the next line . But after second input i got Segmentation fault .Also , what does free(buf) do ? I've seen that the error is at this line : "strcpy(name, strtok(NULL, " "));" This is the code :
#include <stdio.h>
#include <stdlib.h>
int main()
{
char *buf;
char command[32];
char name[32];
while((buf = readline("\n"))!=NULL)
{
if (strcmp(buf,"exit")==0)
break;
printf("%s\n",buf);
strcpy(command, strtok(buf, " "));
printf("%s\n", command);
strcpy(name, strtok(NULL, " "));
printf("%s\n", name);
if(buf[0]!=NULL)
add_history(buf);
}
free(buf);
return 0;
}
readline
do? – Some programmer dudestrtok
output before using it. – Eugene Sh.readline
may be buggy. it's clearer if you show us the code ofreadline
. – Jason Hu