ProgrammingThis 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.
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.
Is there a way to copy a file at run time in a c or c++ app in linux? I am assuming there is something where can execute a system level call such as 'cp' and pass it parameters, but I haven't found anything like that. I found exec, but only in reference to shell scripts.
Is there a way to copy a file at run time in a c or c++ app in linux? I am assuming there is something where can execute a system level call such as 'cp' and pass it parameters, but I haven't found anything like that. I found exec, but only in reference to shell scripts.
#include <fcntl.h>
#include <stdio.h>
#include <sys/stat.h>#define BUFFER_SIZE 256
int main(int argc, char *argv[])
{
//check number of arguments
if (argc != 3)
{
fprintf(stderr, "%s [input file] [output file]\n", argv[0]);
return 1;
}
//open the first file "read-only"
int input = open(argv[1], O_RDONLY);
if (input < 0)
{
fprintf(stderr, "%s: could not read file '%s'\n", argv[0], argv[1]);
return 1;
}
//find the permissions of the first file so we can copy them
int permissions = 0644;
struct stat file_stats;
if (fstat(input, &file_stats) != -1)
permissions = file_stats.st_mode & 07777;
//open the second file "write-only" using the first file's permissions//note: the permissions will only be used if the file doesn't exist
int output = open(argv[2], O_WRONLY | O_CREAT | O_TRUNC, permissions);
if (output < 0)
{
fprintf(stderr, "%s: could not write file '%s'\n", argv[0], argv[2]);
return 1;
}
//create a modestly-sized buffer for holding data
char buffer[BUFFER_SIZE];
ssize_t read_size = 0;
//read as much data as the first file contains
while ((read_size = read(input, buffer, BUFFER_SIZE)) != 0)
{
if (read_size == (ssize_t) -1)
{
fprintf(stderr, "%s: error reading file '%s'\n", argv[0], argv[1]);
return 1;
}
else if (write(output, buffer, read_size) != read_size)
{
fprintf(stderr, "%s: error writing file '%s'\n", argv[0], argv[2]);
return 1;
}
}
return 0;
}
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.