C++ 如何通过C程序打开Cmd(命令提示符)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4885413/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 16:50:40  来源:igfitidea点击:

How to Open Cmd(Command Prompt) through C program

c++cdos

提问by Javed Akram

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.

实际上,我想通过 C 程序执行 DOS 命令,并希望在我的 C 输出窗口中显示 DOS 命令的输出。

example:

例子:

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

使用 "dir C:\" 显示输出到 C- 程序

回答by thkala

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

要在cmd.exe运行 C 程序的同一窗口中执行命令:

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

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

要启动单独的窗口,您需要调用cmd.exe

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

(Note: I have not tested this one);

(注意:我没有测试过这个);

回答by Felice Pollano

system("dir");

should dump in the current stdout

应该转储到当前的标准输出中

回答by Michael Warren

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

但是 system() 是邪恶的。原因如下:http://www.cplusplus.com/forum/articles/11153/确保在使用前仔细考虑。