wetpp
wetpp

Reputation: 21

how to ping a certain ip in a c++ program

Usually I just use the system("ping 8.8.8.8"); command, but I am trying to store information in a variable (IP) and then ping that IP using system in a c++ program.

This seems very easy, but when I tried to execute the code, it tells me there are too many arguments in function call. Can anybody please help me with a solution to this?

ImGui::PushItemWidth(100);
static char IP[64] = ""; ImGui::InputText("PING IP", IP, 64);
ImGui::PopItemWidth();

if (ImGui::Button("ping test")) {
    system("ping ", IP);
}

Upvotes: 0

Views: 3360

Answers (1)

kiler129
kiler129

Reputation: 1144

The system() function takes exactly one argument, as defined by its header file:

int system( const char* command );

It expects a full command string to be executed by the shell.

The easiest way to handle this case will be to concatenate the "ping" string literal and the IP variable. You can do it using std::string instead of char arrays pretty easily in C++:

#include <iostream>
#include <string>
#include <cstdlib>

int main() {
    std::string IP ("127.0.0.1");
    std::string CMD ("ping " + IP);
    std::system(CMD.c_str());
    return 0;
}

Upvotes: 1

Related Questions