简体   繁体   English

使用C ++执行CMD命令

[英]Execute CMD commands using C++

In my project I want to execute some CMD commands. 在我的项目中,我想执行一些CMD命令。 What is the syntax for doing that using C++. 使用C ++执行此操作的语法是什么。

You can execute Windows Command prompt commands using a C++ function called system(); 您可以使用名为system();的C ++函数执行Windows命令提示符命令system(); . For safer standards you are recommended to use Windows specific API'S like ShellExecute or ShellExecuteEx . 为了更安全的标准 ,建议您使用Windows特定的API,如ShellExecuteShellExecuteEx Here is how to run CMD command using system() function. 以下是使用system()函数运行CMD命令的方法。

You should place the CMD command like shown below in the program source code: 您应该将CMD命令放在程序源代码中,如下所示:

system("CMD_COMMAND");

Here is a program which executes the DATE command in CMD to find the date: 这是一个在CMD中执行DATE命令以查找日期的程序:

#include <iostream>
using namespace std;

int main() {
    system("DATE");
    return 0;
}

Use Windows specific APIs: 使用Windows特定的API:

See this also. 看到了这一点

I suppose you could always do: 我想你总能做到:

#include <iostream>
#include <windows.h>

using namespace

int main()
{
    WinExec("cmd", 1);
    return 0;
}

This however, automatically sets the path to the folder your file is in. Just type cd\\ to return to base file. 但是,这会自动设置文件所在文件夹的路径。只需键入cd \\即可返回基本文件。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM