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

Asked

Viewed 182 times

-2

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

inserir a descrição da imagem aqui

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 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;
}
  • 2

    Good thing you didn’t respond using an image instead of code. :)

  • 1

    @Luizvieira Haha! I was going to copy his code to edit but I had to write from 0. I’m glad it’s a tiny code.

  • 1

    Let’s hope he(a) learned his lesson.

  • 1

    I liked it once I wanted to do it and I couldn’t...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.