How to Open Cmd(Command Prompt) through C program

Javed Akram picture Javed Akram · Feb 3, 2011 · Viewed 39.7k times · Source

Actually, I want to execute DOS command by a C program and want to display the output of DOS command in my C Output Window.

example:

use "dir C:\" which displays output to C- program

Answer

thkala picture thkala · Feb 3, 2011

To execute a command in the same cmd.exe window where your C program is running:

#include <stdlib.h>
.
.
.
system("dir C:\\");

To launch a separate windows, you need to call cmd.exe:

system("cmd.exe /c dir c:\\");

(Note: I have not tested this one);