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:\t.\r.\n.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:
This can sometimes lead to obscure errors with commands like:
where the
\tis actually thetabcharacter and the file that you’re trying to open is:c:TABext.dat.