HTTP 요청을 받아 소량의 데이터를 추출하는 함수를 작성하려고합니다. 내 기능은 다음과 같습니다 : 분명히 잘못 GET /?f=fibGET /favicon.ico HTTP/1.1
로 strtok()
, ftoken
인쇄를 호출 한 후, 현재 GET /?f=fib&n=10 HTTP/1.1
strtok()을 사용하여 C로 문자열 토큰 화하기
:
char* handle_request(char * req) {
char * ftoken; // this will be a token that we pull out of the 'path' variable
// for example, in req (below), this will be "f=fib"
char * atoken; // A token representing the argument to the function, i.e., "n=10"
...
// Need to set the 'ftoken' variable to the first arg of the path variable.
// Use the strtok function to do this
ftoken = strtok(req, "&");
printf("ftoken = %s", ftoken);
// TODO: set atoken to the n= argument;
atoken = strtok(NULL, "");
printf("atoken = %s", atoken);
}
req
f=fib
이고
atoken
은
n=10
일 것입니다 누군가 제가 이것을 알아내는 데 도움이 될 수 있습니까?
어떻게해야 내가 여기에 대신 롤 strtok''피할 것이다 – fatrock92
같은 출력보기 내 자신의 기능. – dreamlax
'strtok'은 놀랍습니다. 나는 이것을 거의 매일 사용한다. 문자열 파싱을위한 또 다른 좋은 기능은'sscanf'입니다. – fatrock92