system()复制失败,而cmd复制工作 [英] system() copy fails, while cmd copy works
问题描述
在cmd.exe中,我可以执行命令copy c:\hello.txt c:\hello2.txt,它工作正常。
但在我的C程序,我运行这段代码,并得到以下错误:
#include< iostream>
using namespace std;
int main()
{
system(copy c:\hello.txt c:\hello2.txt);
system(pause);
return 0;
}
输出:
系统找不到指定的文件。 p>
任何人都知道这是怎么回事。
和许多使用相同转义规则的其他语言), \
应该是 \\
转义字符。它允许您在正常文本中输入不可打印的字符,例如:
- 标签字符
\ t
。 - 回车字符
\r
。 - 换行符
\\\
。
- 其他我不会详细介绍。
由于 \
用作转义字符,我们需要一种方法,实际 '\'
。这是通过序列 \\
来完成的。
因此你的行应该是:
系统(copy c:\\hello.txt c:\\hello2.txt);
这有时可能会导致以下命令的模糊错误:
FILE * fh = fopen(c:\text.dat,w);
其中 \t
标签
字符,您尝试打开的文件是:
; c : TAB e x t t 。
In cmd.exe, I can execute the command "copy c:\hello.txt c:\hello2.txt" and it worked fine. But in my C program, I ran this piece of code and got the following error:
#include <iostream>
using namespace std;
int main()
{
system("copy c:\hello.txt c:\hello2.txt");
system("pause");
return 0;
}
Output: The system cannot find the file specified.
Anybody know what is going on here?
Inside C strings (and quite a few other languages that use the same escaping rules), \
should be \\
since it's the escape character. It allows you to enter, in normal text, non-printable characters such as:
- the tab character
\t
. - the carriage-return character
\r
. - the newline character
\n
. - others which I won't cover in detail.
Since \
is used as the escape character, we need a way to put an actual '\'
into a string. This is done with the sequence \\
.
Your line should therefore be:
system("copy c:\\hello.txt c:\\hello2.txt");
This can sometimes lead to obscure errors with commands like:
FILE *fh = fopen ("c:\text.dat", "w");
where the \t
is actually the tab
character and the file that you're trying to open is:
c:TABext.dat.
这篇关于system()复制失败,而cmd复制工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!