在此代码段中:
printf("shell> ");
fgets(input, MAX_INPUT_SIZE, stdin);
//tokenize input string, put each token into an array
char *space;
space = strtok(input, " ");
tokens[0] = space;
int i = 1;
while (space != NULL) {
space = strtok(NULL, " ");
tokens[i] = space;
++i;
}
//copy tokens after first one into string
strcpy((char*)cmdargs, ("%s ",tokens[1]));
for (i = 2; tokens[i] != NULL; i++) {
strcat((char*)cmdargs, ("%s ", tokens[i]));
}
printf((char*)cmdargs);
输入: echo hello world and stuff
,程序打印:
helloworldandstuff
在我看来,该行strcat((char*)cmdargs, ("%s ", tokens[i]));
应该将 tokens[i] 处的字符串与后面的空格连接起来。strcat 不适用于字符串格式吗?任何其他想法可能会发生什么?