Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Open Cmd(Command Prompt) through C program

Tags:

c++

c

dos

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

like image 524
Javed Akram Avatar asked Feb 03 '11 11:02

Javed Akram


3 Answers

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);

like image 102
thkala Avatar answered Sep 26 '22 03:09

thkala


system("dir");

should dump in the current stdout

like image 26
Felice Pollano Avatar answered Sep 25 '22 03:09

Felice Pollano


But system() is evil. Here's why: http://www.cplusplus.com/forum/articles/11153/ Make sure you give thorough thought before using it.

like image 23
Michael Warren Avatar answered Sep 23 '22 03:09

Michael Warren