-2
#include <iostream>
#include <string>
using std::string;
using std::system;
int main()
{
string line;
string command = "ping ";
string param = "www.google.com";
line = command + param;
system(line.c_str());
return 0;
}
Alternatively, you may concatenate one std::string
and call .c_str()
directly without a third temporary variable. Second [class.Temporary]/4, the lifetime of the temporary created by concatenation goes to the end of the function call, so it is safe to get a pointer to the first element of the string with .c_str()
.
using std::string;
int main()
{
string arg = "www.google.com";
system(("ping " + arg).c_str());
return 0;
}
Read that question and that answer that in it is.
– Victor Stafusa