0

我有这些变量:

char* name = "bobsux";    
int score = 100;
char* scoreHash = "f899139df5e1059396431415e770c6dd";

使用格式说明符和 printf 打印这些,返回:

printf("name=%s&score=%d&score_hash=%s", name, score, scoreHash);
=> name=bobsux&score=100&score_hash=f899139df5e1059396431415e770c6dd

如何制作返回相同的变量?尝试失败:

char* scoreData = ("name=%s&score=%d&score_hash=%s", name, score, scoreHash);
=> f899139df5e1059396431415e770c6dd

我希望变量类似于 printf 返回,以便我可以将 scoreData 发送到 Web 服务器:

curl_easy_setopt(curl, CURLOPT_POSTFIELDS, scoreData);
4

1 回答 1

5

您用于snprintf()将文本格式化为字符数组:

char scoreData[128];

snprintf(scoreData, sizeof scoreData, "name=%s&score=%d&score_hash=%s\n",
         name, score, scoreHash);
于 2012-10-22T12:58:54.187 回答