command line arguments using system function

I am having issues with sending arguments to the command prompt using the system function to copy and paste a file. Here is my code:

system(" \"\"copy c:\\users\\william powell\\downloads\\rd.txt\" \"c:\\users\\william powell\\downloads\\setup files\" ");

This is the error message:
The filename,directory name or volume label syntax is incorrect.

cannot figure out what the issue is
thanks in advance.
Last edited on
It looks like you have mis-matched quotes.

After creating a string and printing it out.
""copy c:\users\william powell\downloads\rd.txt" "c:\users\william powell\downloads\setup files"

if you are on windows, you can use CopyFile function i think the name was
I think you want to quote the file names, but not the "copy" command itself:
system("copy \"c:\\users\\william powell\\downloads\\rd.txt\" \"c:\\users\\william powell\\downloads\\setup files\" ");
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <fstream>
#include <string>

bool copy_file( std::string source_file_name, std::string dest_file_name, bool append = false )
{
    std::ifstream srce( source_file_name ) ;
    std::ofstream dest( dest_file_name, append ? std::ios_base::app : std::ios_base::out ) ;
    return bool( dest << srce.rdbuf() ) ;
}

int main()
{
    if( !copy_file( __FILE__, "main.cpp.bak" ) )
    {
        std::cerr << "copy failed\n" ;
        return 1 ;
    }
}

http://coliru.stacked-crooked.com/a/1161e2c143693a90
@dhayden that worked thanks but can you explain why does copy has to be outside the quotes
Because you want the shell to see
copy "c:\users\william powell\downloads\rd.txt" "c:\users\william powell\downloads\setup files"

instead of
"copy c:\users\william powell\downloads\rd.txt" "c:\users\william powell\downloads\setup files"

You're quoting the arguments to the copy command. They are quoted because they contain spaces.
Topic archived. No new replies allowed.