1

HTTP リクエストを受け取り、少量のデータを抽出する関数を作成しようとしています。私の関数は次のようになります。

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通常は次のようになります。GET /?f=fib&n=10 HTTP/1.1

現在、 を呼び出した後strtok()、明らかに間違っているとftoken出力されます。GET /?f=fibGET /favicon.ico HTTP/1.1理想的には、そうなるでしょうf=fibし、atokenそうなるでしょうn=10

4

1 に答える 1

1

入力 ->GET /?f=fib&n=10 HTTP/1.1

出力 -> ftokenf=fibと atoken10

コード ->

ftoken = strtok(req, "?"); // This tokenizes the string till ?
ftoken = strtok(NULL, "&"); // This tokenizes the string till & 
                            // and stores the results in ftoken
printf("ftoken = %s", ftoken); // Result should be -> 'f=fib'

atoken = strtok(NULL, "="); // This tokenizes the string till =.
atoken = strtok(NULL, " "); // This tokenizes the string till next space.
printf("atoken = %s", atoken); // Result should be -> 'n=10'
于 2013-02-26T03:45:46.513 に答える