Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Suppress system("ping") output in C++

I have written a simple program that pings three sites and then reacts to whether they are reachable or not.

My question is: can I suppress system("ping ")'s output? I have written my code in C++ as I know that language the best. Currently the code opens the ping.exe running the system command. If I can prevent the output from showing up while it still pings that would be ideal.

I am eventually going to turn this program in a windows service that is why I would like to suppress both the command line console window as well as suppress the ping output. Thanks.

like image 215
Samuel Avatar asked Oct 31 '10 06:10

Samuel


3 Answers

Try doing system("ping host > nul") (nul is windows equivalent of UNIX /dev/null).

like image 151
zvrba Avatar answered Oct 17 '22 07:10

zvrba


Generally, if you're going to call another program but don't want it to act like std::system, you're going to need a platform-specific function like fork()/exec() on UNIX or CreateProcess() on Windows. These functions give you control over how the other program runs, for instance, that it not show output or not create a console window, etc.

like image 40
Max Lybbert Avatar answered Oct 17 '22 08:10

Max Lybbert


You can use system command like below to suppress the output of ping command.

system("ping 100.100.100.100 > response.dat");

Above command pings IP address 100.100.100.100 and directs the output to a file called response.dat. In response.dat you can see the response of ping command.

like image 36
bjskishore123 Avatar answered Oct 17 '22 07:10

bjskishore123