I'm having trouble with splitting strings and the proper use of the function strtok:
Here's my problem:
I'm reading from stdin, and i want to split the input the following way:
<command> <key> <data>
"PUT 122gyhbhb having trouble with splitting strings and the proper use of the function streak:"
the first word(PUT) is the command, the second is the key and the other words until the end of input are the data.
Here's what i've done so far:
char *buffer = (char *) malloc(sizeof(char) * 2048);
fgets(buffer,2048, stdin);
char *options = strtok(buffer, " ");
char *key = strtok(NULL," ");
char *data = strtok(NULL, " ");
However this gets me the command, the key, but only the first word of the data. How can i change this, so it does what i want ? Thanks!
您需要更改最后一次调用strtok
的分隔符:
char *data = strtok(NULL, "\n");
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.