如何在c ++中使用CMD命令? [英] How to use a CMD command in c++?
问题描述
我要使用此cmd命令
ROBOCOPY D:\folder1 D:\folder2 /S /E
具有将folder1的内容复制到folder2的条件
with conditions to copy the contents of folder1 to folder2
if(i == 1)
if(i == 2)
ROBOCOPY D:\folder3 D:\folder4 /S /E
将folder3的内容复制到folder4
to copy the contents of folder3 to folder4
我该怎么办?
推荐答案
do?
只需执行此操作(使用 std :: system()
:
You simply do this (using the std::system()
function):
#include <cstdlib>
// ...
if(i == 1) {
std::system("ROBOCOPY D:/folder1 D:/folder2 /S /E");
}
else if(i == 2) {
std::system("ROBOCOPY D:/folder3 D:/folder4 /S /E");
}
请注意,对于字符串字面量如D:\folder3
,您需要转义'\'
code>'\':D:\\folder3
。
或两更多,取决于解释命令shell(应该在窗口上工作 cmd 不这样做):D:\\\\\folfol3
。
更简单的方法是使用更容易编写'/'
字符,也可以用于指定最近的窗口。
Note that for string literals like "D:\folder3"
, you'll need to escape '\'
characters, with another '\'
: "D:\\folder3"
.
Or even two more, depending on the interpreting command shell (should work on windows cmd without doing so): "D:\\\\folder3"
.
The easier way though, is to use the simpler to write '/'
character, that's accepted for specifying windows pathes lately as well.
这篇关于如何在c ++中使用CMD命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!