0

連結を回避するために、snprintf に似たメッセージを作成するにはどうすればよいですか (ここでは、整数の %d を含む一般的なテキストを使用でき、sprintf connect with パラメータで表示する必要がある場合にのみ使用できます)。(結果文字列のようなものを作成する必要がありますYou need more %d coins。現時点では、連結して値を返すのが悪い方法です 'You need more' + some_stringified_value + 'coins'

4

2 に答える 2

1

C++ でもsnprintfを使用できます。

int snprintf ( char * s, size_t n, const char * format, ... );

例(前述のリンクから):

/* snprintf example */
#include <stdio.h>

int main ()
{
  char buffer [100];
  int cx;

  cx = snprintf ( buffer, 100, "The half of %d is %d", 60, 60/2 );

  snprintf ( buffer+cx, 100-cx, ", and the half of that is %d.", 60/2/2 );

  puts (buffer);

  return 0;
}

出力:

The half of 60 is 30, and the half of that is 15.
于 2013-08-07T13:12:22.143 に答える