I am working on creating a simple version of Minix. I am using fgets()
to grab user input. I am then using strtok()
to split the string up with the delimiter " "
. The problem is when I call strtok(NULL, " ")
, my token stored appends a space to the last char. So if I pass "minimount imagefile.img"
, my program will grab minimount
and store it in variable cmd
, then it will grab "imagefile.img "
and place it in variable flag
. Notice the space at the end of the flag
variable is added after the token method.
Is there a way for me to grab just the string without a space at the end after token is called. Or is there a way to manipulate the string to remove the appended space?
printf("Minix: ");
fgets(cmd, BUFFLIM, stdin);
//parses string using delimiter " "
char *token = strtok(cmd, " ");
//assigns flag to what is after delimiter
char *f = strtok(NULL, " ");
//printf("cmd:%s\nf:%s\n", cmd, f);
printf("cmd:%s\nf:%s", cmd, f);
Output:
cmd:"minimount"
f:"imagefile.img "