아래의 코드 조각과 관련된 문제점은 무엇입니까? 그것은 라인 # 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를 계산하는 거 야? –