{steinsoft.net}
 
 
 
 
 
Home/News
Code Snippets
Board
Projects
What's That?
I am using it actively
I planning to use/learn it
I don't plan to lean/use it
5th option
Leipzig
Home :: Programming :: Code Snippets :: Cpp :: Execute System Commands

[ Execute System Commands ]

In this short code snippet I will show you how to execute system commands like ping or nslookup or format c: ;-). The function we will use is rather intended to be used in textconsole based applications. But you could also use it in other programs.

That is our 'magic' function. You have to include stdlib.h to use it.

int system( const char *command );

command is a string that holds the app's name with optional arguments. It is used like on the commandline. The good thing is that the text output that the application produces will be shown in your textconsole. You could now use it to program your own shell! Nice, isn't it? The return-value will be same as the the return-value of the system command only if there was an error, then it will be -1. Here's a little example:

#include <stdlib.h>
#include <iostream.h>
 
int main()
{
   cout << "Executing ping..." << endl;
   system("ping localhost");
   cout << "Ping is done. I am done too. Let's shutdown and go for a drink."
        << endl;
}

This simple code snippet outputs some text and then starts ping with argument localhost. The ouput of ping will be shown in your textconsole and it will be the same as if you run it on the shell.

So I hope you will be able now to develop a next generation shell. If you have further problems, simply m@il me. Happy coding!

 Last edited 2002-12-07 18:46:29 by André Stein - printable version
» copyright by andré stein
» using stCM v1.0
» steinsoft.net revision 5.0