Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
05-02-2005, 01:08 PM
|
#1
|
Member
Registered: Dec 2004
Location: India
Distribution: Fedora Core 4
Posts: 145
Rep:
|
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
|
|
|
05-02-2005, 01:28 PM
|
#2
|
Moderator
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,794
|
Quote:
But this is not working effectively in all cases.
|
In what cases ?
|
|
|
05-02-2005, 01:47 PM
|
#3
|
Senior Member
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246
Rep:
|
How about something like this?
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
int main(int argc, char **argv)
{
int fdin, fdout;
struct stat st;
char buf[BUFSIZ];
int n_read;
if(argc != 3)
{
puts("Usage: mycp <source> <destination>");
exit(1);
}
if((fdin = open(argv[1], O_RDONLY)) == -1)
{
printf("open(): %s\n", strerror(errno));
exit(1);
}
if(fstat(fdin, &st) == -1)
{
printf("stat(): %s\n", strerror(errno));
exit(1);
}
umask(0);
if((fdout = open(argv[2], O_WRONLY|O_CREAT|O_TRUNC, st.st_mode)) == -1)
{
printf("open(): %s\n", strerror(errno));
exit(1);
}
while((n_read = read(fdin, buf, sizeof(buf))) > 0)
{
if(write(fdout, buf, n_read) == -1)
{
printf("write(): %s\n", strerror(errno));
exit(1);
}
}
close(fdin);
close(fdout);
if(n_read == -1)
{
printf("read(): %s\n", strerror(errno));
exit(1);
}
return 0;
}
|
|
|
05-02-2005, 03:15 PM
|
#4
|
Member
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557
Rep:
|
The cases where a filename contains a shell metacharactor or spaces, and where your host doesn't have cp.
And no, putting quotes around the %s in the sprintf statement doesn't protect against all special characters : )
|
|
|
05-02-2005, 11:27 PM
|
#5
|
Member
Registered: Nov 2004
Posts: 72
Rep:
|
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
|
|
|
05-03-2005, 01:09 AM
|
#6
|
Moderator
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,794
|
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.
|
|
|
05-03-2005, 11:25 AM
|
#7
|
Senior Member
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246
Rep:
|
Just use the code I posted then. It doesn't require talking to the shell to get the job done.
|
|
|
05-03-2005, 01:38 PM
|
#8
|
Moderator
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,794
|
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 ...
|
|
|
05-03-2005, 02:29 PM
|
#9
|
Senior Member
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246
Rep:
|
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 09:48 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|