如何使用cpp在cmd提示符上运行命令 [英] How to run commands on cmd prompt using cpp

查看:785
本文介绍了如何使用cpp在cmd提示符上运行命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只需打开一个cmd提示符并将命令传递给cmd提示符并运行它们。

例如

1.open cmd

2.通过命令更改目录

3.通过命令打开文件。



什么我试过了:



我试过如下:

All I need is to open a cmd prompt and pass commands to cmd prompt and run them.
For example
1.open cmd
2.pass a command to "change directory"
3.pass a command to open a file.

What I have tried:

I've tried as below:

#include <iostream>
int main()
{
	std::cout << "Trying to run cmd using runas cmd";
	system("cmd;");

	return 0;
}





它会打开cmd提示符,但我需要将命令传递给cmd并让它们运行。



我试过在C ++中使用system()运行2个或更多cmd命令 - Stack Overflow [ ^ ]



as system( cmd; cd /; d:;);但引发错误..请帮助我..



It opens cmd prompt but I need to pass commands to cmd and make them run.

I've tried as in run 2 or more cmd command using system() in C++ - Stack Overflow[^]

as system("cmd; cd/; d:;"); but raises error..Please help me..

推荐答案

system()函数已调用命令处理器 cmd 因此,如果您不想让用户输入命令或设置特定选项,则无需再次执行。



随着Windows命令提示,不支持使用';'来分隔命令。如果命令以非零值返回,则使用'&'或'&&'代替后者将停止执行。



检查命令打开Windows命令提示符窗口并输入它们。一旦他们执行了您想要的操作,就将相同的字符串传递给 system()函数。
The system() function already invokes the command processor cmd so that there is no need to execute it again if you not want to let the user enter commands or set specific options.

With Windows command prompts, using ';' to separate commands is not supported. Use '&' or '&&' instead where the latter will stop execution if a command returns with a non-zero value.

To check your commands open a Windows command prompt window and enter them. Once they do what you want pass the same string to the system() function.


这篇关于如何使用cpp在cmd提示符上运行命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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