开发者

snprintf with "%0*d" , too few arguments

开发者 https://www.devze.com 2023-04-11 01:26 出处:网络
I needed to dynamically generate a string like the following: 001 / 192 However , the number of digits varies , thus i must define another variable , called print_width , which is , in this case:

I needed to dynamically generate a string like the following:

001 / 192

However , the number of digits varies , thus i must define another variable , called print_width , which is , in this case: 192 % 10

snprintf ( statusString , 30 , "%0*d / %0*d" , snprintf_width , completed[tid] , total[tid]);

The code above posed a compiler wa开发者_如何学Crning: too few arguments , and not working


You need an integer width argument for each * as well as for each d in the format. You've provided 3 of the requisite 4 arguments, and your compiler is kind enough to tell you, rather than producing garbage at run time.

Hence, for example:

if (snprintf(statusString, sizeof(statusString), "%0*d / %0*d", snprintf_width,
             completed[tid], snprintf_width, total[tid]) >= sizeof(statusString))
    throw "Oops - string was too small for data";


you need the width twice (you have * twice)

0

精彩评论

暂无评论...
验证码 换一张
取 消