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?
This question is related to
c#
c++
string
string-formatting
The C++ way would be to use a std::stringstream
object as:
std::stringstream fmt;
fmt << a << " " << b << " > " << c;
The C way would be to use sprintf
.
The C way is difficult to get right since:
Of course, you may want to fall back on the C way if performance is an issue (imagine you are creating fixed-size million little stringstream
objects and then throwing them away).
You can just concatenate the strings and build a command line.
std::string command = a + ' ' + b + " > " + c;
system(command.c_str());
You don't need any extra libraries for this.
As already mentioned the C++ way is using stringstreams.
#include <sstream>
string a = "test";
string b = "text.txt";
string c = "text1.txt";
std::stringstream ostr;
ostr << a << " " << b << " > " << c;
Note that you can get the C string from the string stream object like so.
std::string formatted_string = ostr.str();
const char* c_str = formatted_string.c_str();
For the sake of completeness, you may use std::stringstream
:
#include <iostream>
#include <sstream>
#include <string>
int main() {
std::string a = "a", b = "b", c = "c";
// apply formatting
std::stringstream s;
s << a << " " << b << " > " << c;
// assign to std::string
std::string str = s.str();
std::cout << str << "\n";
}
Or (in this case) std::string
's very own string concatenation capabilities:
#include <iostream>
#include <string>
int main() {
std::string a = "a", b = "b", c = "c";
std::string str = a + " " + b + " > " + c;
std::cout << str << "\n";
}
For reference:
If you really want to go the C way. Here you are:
#include <iostream>
#include <string>
#include <vector>
#include <cstdio>
int main() {
std::string a = "a", b = "b", c = "c";
const char fmt[] = "%s %s > %s";
// use std::vector for memory management (to avoid memory leaks)
std::vector<char>::size_type size = 256;
std::vector<char> buf;
do {
// use snprintf instead of sprintf (to avoid buffer overflows)
// snprintf returns the required size (without terminating null)
// if buffer is too small initially: loop should run at most twice
buf.resize(size+1);
size = std::snprintf(
&buf[0], buf.size(),
fmt, a.c_str(), b.c_str(), c.c_str());
} while (size+1 > buf.size());
// assign to std::string
std::string str(buf.begin(), buf.begin()+size);
std::cout << str << "\n";
}
For reference:
Then, there's the Boost Format Library. For the sake of your example:
#include <iostream>
#include <string>
#include <boost/format.hpp>
int main() {
std::string a = "a", b = "b", c = "c";
// apply format
boost::format fmt = boost::format("%s %s > %s") % a % b % c;
// assign to std::string
std::string str = fmt.str();
std::cout << str << "\n";
}
In addition to options suggested by others I can recommend the fmt library which implements string formatting similar to str.format
in Python and String.Format
in C#. Here's an example:
std::string a = "test";
std::string b = "text.txt";
std::string c = "text1.txt";
std::string result = fmt::format("{0} {1} > {2}", a, b, c);
Disclaimer: I'm the author of this library.
For completeness, the boost way would be to use boost::format
cout << boost::format("%s %s > %s") % a % b % c;
Take your pick. The boost solution has the advantage of type safety with the sprintf
format (for those who find the <<
syntax a bit clunky).
Source: Stackoverflow.com