使用 C++ 执行 CMD 命令 [英] Execute CMD commands using C++

查看:85
本文介绍了使用 C++ 执行 CMD 命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的项目中,我想执行一些 CMD 命令.使用 C++ 执行此操作的语法是什么.

In my project I want to execute some CMD commands. What is the syntax for doing that using C++.

推荐答案

您可以使用名为 system(); 的 C++ 函数执行 Windows 命令提示命令.为了更安全的标准,建议您使用 Windows 特定的 API,例如 ShellExecuteShellExecuteEx.下面是如何使用 system() 函数运行 CMD 命令.

You can execute Windows Command prompt commands using a C++ function called system();. For safer standards you are recommended to use Windows specific API'S like ShellExecute or ShellExecuteEx. Here is how to run CMD command using system() function.

您应该在程序源代码中放置如下所示的 CMD 命令:

You should place the CMD command like shown below in the program source code:

system("CMD_COMMAND");

这是一个在 CMD 中执行 DATE 命令来查找日期的程序:

Here is a program which executes the DATE command in CMD to find the date:

#include <iostream>
using namespace std;

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

这篇关于使用 C++ 执行 CMD 命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆