7

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

2
  • You should use pipe, no idea how to do this in Windows, in UNIX systems you use popen. There must be some equivalent for this in Windows Commented Feb 3, 2011 at 11:18
  • Sorry, the pipe is needed only if you want to use the output in you program, forgot to mention that. Otherwise, you can use system(".."), as the other answers suggest. Commented Feb 3, 2011 at 11:26

3 Answers 3

8

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

Sign up to request clarification or add additional context in comments.

1 Comment

Does not work on windows 10 just outputs to same window.
3
system("dir");

should dump in the current stdout

Comments

2

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.