0

I have these variables:

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

Printing these using format specifiers and printf, returns this:

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

How can I make a variable that returns the same? Failed attempt:

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

I want the variable to be like the printf return, so that I can send scoreData to a web server:

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 に答える