I want to use a system call to copy (cp) a source file to a backup directory. I need to pass the source and destination paths via command prompt, but the only way I've been able to get the system call to work is by hard coding it (below). Can this be done?
Working line of code:
Attempt to pass path via variable:Code:system("cp -r /home/myuser/prog.c /home/myuser/backup/prog.c");
The output from the non-working attempt is:Code:snprintf(destpath, MAX_LEN, "\"cp -r /home/myuser/prog.c /home/myuser/backup/prog.c\""); printf("This is the destpath: %s\n", destpath); system(destpath);
sh: cp -r /home/myuser/prog.c /home/myuser/backup/prog.c: not found.
However, I can take the sh: error, copy the cp command and paste it on a unix prompt and successfully copy the file from source to destination.