How to pass data read in a program in c ++ like momando in cmd

-2

I would like to know how to read something in a program and pass this reading as a command to cmd, through the stdlib or cstdlib library, whatever. The photo shows what I'm trying to do.

    
asked by anonymous 11.02.2017 / 22:11

1 answer

3
#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 can concatenate a std::string and call .c_str() directly without a third temporary variable. According to [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;
}
    
12.02.2017 / 22:55