Subscribe Now: standard

Thursday, October 20, 2011

How to run system commands in C++ or C


Hello dear tech witches and wizards.

Welcome in to today's post we will be attacking a very simple question.
How do we execute system commands (DOS commands, shell commands ..etc) from a c++ or c program? This is a very common question that come up for new programmers who have finished learning the programming language and is looking forward to create something useful that can be integrated with system or some other software. So lets solve it.

The solution for this lies in the header file:
stdlib.h
So include the library file with the command #include

The function that will help us with this is:
system();
Which can be used as:    system("Command Within Quotes");

So let's say if  you want the system to list the files in current directory you can try:
system("dir");        //for WINDOWS
&
system("ls");        //for Linux

It is also possible to run commands stored in an array:
like
char ray[50] = "ping yahoo.com";
system(ray);
Will execute the command stored in the array.
Hope this helps some of you at some time.

That's  all folks till we meet again.
 
 
 
 
 
 

No comments:

Post a Comment