我想拆分从终端输入收到的字符串,如果它们包含在缓冲区中。如果他们是我想打印它们。拆分字符串在C strtok()
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* fich[5]={"ha","he","hi","ho","hu"};
int main(){
char passaarg[70];
const char space[2]= " ";
char *token;
int i;
while(1){
read(STDIN_FILENO, passaarg, 70);
for (i = 0; i < 5; i++){
if(strncmp(passaarg, fich[i], 2) == 0){
token = strtok(passaarg, space);
while(token != NULL){
printf("%s\n", token);
token = strtok(NULL, space);
printf("%s\n", token);
}
break;
}
}
}
return 0;
}
我的输出如下一个:提前
ha he
ha
he
he
Segmentation fault (core dumped)
谢谢!
你的第二个'printf'最终将尝试打印'NULL'字符串。 – 2014-12-05 15:24:29