下面的代碼片段有什麼問題;它在#25行失敗。我不明白爲什麼它失敗。儘管正確調用strtok失敗
#include <stdio.h>
#include <string.h>
char linebuf[1024]="SET SLMSRVR 10.133.2.102: 50011";
char *tempStr;
char *str;
int main()
{
tempStr = calloc(1024, sizeof(char));
strcpy(tempStr, linebuf+strlen("SET SLMSRVR"));
printf("1 tempStr: %s\r\n", tempStr);
str = strtok(tempStr, ":");
printf("2 tempStr: %s\tstr: %s\r\n", tempStr, str);
if (str != NULL){
printf("Server IP: %s\r\n",str);
} else {
printf("Error 1\r\n");
}
str = strtok(tempStr, NULL);
printf("3 tempStr: %s\tstr: %s\r\n", tempStr, str);
if (str != NULL) {
printf("Port: %s\r\n", str);
}
return 0;
}
這裏作爲的strtok的規範建議,首先它被稱爲與分隔符串,然後用NULL
,在這兩種情況下的第一個參數的字符串進行解析。它第二次解析失敗。
有什麼想法?
而且我們應該算線#25? –