2012-10-22 113 views
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); 

回答

5

您使用snprintf()的文本字符數組的格式:

char scoreData[128]; 

snprintf(scoreData, sizeof scoreData, "name=%s&score=%d&score_hash=%s\n", 
     name, score, scoreHash);