我有这些变量:
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);
答案 0 :(得分:5)
使用snprintf()
将文本格式化为字符数组:
char scoreData[128];
snprintf(scoreData, sizeof scoreData, "name=%s&score=%d&score_hash=%s\n",
name, score, scoreHash);