Since you’re using C++, why not use stringstream
to build your buffer in pieces:
#include <cstdio>
#include <sstream>
#include <string>
using namespace std;
int main() {
string s1 = "s1";
string s2 = "s2";
string s3 = "s3";
int x = 0;
stringstream ss;
ss << s1 << "," << s2 << "," << s3;
if (x != 0)
ss << "," << x;
ss << " " << endl;
// Don't do this! See link below
//const char* c = ss.str().c_str();
string result = ss.str();
const char* c = result.c_str();
printf("Result: '%s'\n", c);
getchar();
return 0;
}
15
solved How to not print integer when its value is zero? [closed]