简体   繁体   English

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

[英]How to Open Cmd(Command Prompt) through C program

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程序

To execute a command in the same cmd.exe window where your C program is running: 要在运行C程序的同一cmd.exe窗口中执行命令:

#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); (注意:我没有测试过这个);

But system() is evil. 但是system()是邪恶的。 Here's why: http://www.cplusplus.com/forum/articles/11153/ Make sure you give thorough thought before using it. 原因如下: http: //www.cplusplus.com/forum/articles/11153/确保在使用前仔细考虑。

system("dir");

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用C ++与命令提示符(CMD)进行通信? - How can I communicate with Command Prompt (CMD) using C++? 如何从 C++ 程序运行命令提示符 - How to run Command Prompt from a C++ program 从命令提示符执行C ++程序 - Execute C++ program from command prompt 通过命令提示符编译C ++项目 - Compiling c++ project through the command prompt 如何在Windows命令提示符下使用Dev C ++编译器编译c ++程序? - How to compile a c++ program using Dev C++ compiler in windows command prompt? 为什么当在多个文件中通过命令提示符运行该C ++程序时会崩溃,而在单个文件上运行时却不会崩溃? - Why this C++ program crashes specifically when run through command prompt on multiple files but not when run on individual files? 如何通过 C++ 程序在批处理文件上编写一些命令? - How to write some command on a batch file through C++ program? 如何使用命令提示符、记事本和 MinGW 编译带有主 header 和实现文件的 C++ 程序? - How to compile C++ program with a main, header and implementation file using command prompt, notepad and MinGW? 如何在Windows10的命令提示符下为opencv 3.0 c ++程序生成.exe? - How to generate .exe for opencv 3.0 c++ program from command prompt in windows10? 使用system()函数在C ++程序中运行cmd命令 - Running a cmd command within a C++ program using system() function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM