how to copy files using C
I need to copy a file to some location (in my project). I'm doing this using the system function like
char *cmd; sprintf(cmd,"cp %s <dest>",args); system(cmd); But this is not working effectively in all cases. Is there some way out to do this??????????????? pls help me out.................................................. Thanx |
Quote:
|
How about something like this?
Code:
#include <stdio.h> |
Quote:
And no, putting quotes around the %s in the sprintf statement doesn't protect against all special characters : ) |
hi,
char *cmd; sprintf(cmd,"cp %s <dest>",args); i guess this should not work in any class, because you are not allocating memory for cmd, you are directly printing it to the some location contained by cmd. so do it following way char *cmd; cmd = (char *)malloc(<size>); sprintf(cmd,"cp %s <dest>",args); this should work Regards Mehul |
This wouldn't address the special characters issue, you can avoid most of them with "cp '%s' <dest>".
Still wrong when args is more than one filename, or contain itself the single quote character. |
Just use the code I posted then. It doesn't require talking to the shell to get the job done.
|
Technically, the code you posted can only be used from a shell, so is not exactly adressing the issue.
Of course, slightly modifying it to make a usable function of it will fill the gap ... |
Or making it prompt the user for the source and destination filenames will also fix it. At any rate, it doesn't depend on system(), so it doesn't matter if the host doesn't have the cp command or whatever.
|
All times are GMT -5. The time now is 05:33 PM. |