Yury Pogrebnyak Yury Pogrebnyak - 1 year ago 82
C++ Question

Build string as in printf function

printf("%d.%d.%d", year, month, day);

Can I do the same but without printing, smth like

char* date = "%d.%d.%d", year, month, day;

Or maybe some other simple ways to do that?

Answer Source

In plain c there is asprintf() which will allocate memory to hold the resulting string:

#include <stdio.h>
char *date;
asprintf(&date, "%d.%d.%d", year, month, day);

(error handling omitted)

Since you have tagged C++ you probably want to use the C++ solutions.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download