String.Format alternative in C++
c#, c++, string, string-formatting
Solution
You can use `sprintf` in combination with `std::string.c_str()`.
`c_str()` returns a `const char*` and works with `sprintf`:
string a = "test";
string b = "text.txt";
string c = "text1.txt";
char* x = new char[a.length() + b.length() + c.length() + 32];
sprintf(x, "%s %s > %s", a.c_str(), b.c_str(), c.c_str() );
string str = x;
delete[] x;
or you can use a pre-allocated `char` array if you know the size:
string a = "test";
string b = "text.txt";
string c = "text1.txt";
char x[256];
sprintf(x, "%s %s > %s", a.c_str(), b.c_str(), c.c_str() );
Problem
I don't have much experience working with C++. Rather I have worked more in C# and so, I wanted to ask my question by relating to what I would have done in there. I have to generate a specific format of the string, which I have to pass to another function. In C#, I would have easily generated the string through the below simple code. ``` string a = "test"; string b = "text.txt"; string c = "text1.txt"; String.Format("{0} {1} > {2}", a, b, c); ``` By generating such an above string, I should be able to pass this in `system()`. However, `system` only accepts `char*` I am on `Win32 C++` (not C++/CLI), and cannot use `boost` since it would include too much inclusion of all the files for a project which itself is very small. Something like `sprintf()` looks useful to me, but `sprintf` does not accept `string` as the `a`, `b` and `c` parameters. Any suggestions how I can generate these formatted strings to pass to system in my program?