Вызов a shell с использованием system () неэффективен и не очень безопасен.
Самый эффективный способ скопировать файл в Linux - использовать системный вызов sendfile () .В Windows следует использовать CopyFile () API или один из связанных с ним вариантов.
Пример с использованием sendfile :
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/sendfile.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
int main (int argc, char* argv[])
{
int read_fd;
int write_fd;
struct stat stat_buf;
off_t offset = 0;
/* Open the input file. */
read_fd = open (argv[1], O_RDONLY);
/* Stat the input file to obtain its size. */
fstat (read_fd, &stat_buf);
/* Open the output file for writing, with the same permissions as the
source file. */
write_fd = open (argv[2], O_WRONLY | O_CREAT, stat_buf.st_mode);
/* Blast the bytes from one file to the other. */
sendfile (write_fd, read_fd, &offset, stat_buf.st_size);
/* Close up. */
close (read_fd);
close (write_fd);
return 0;
}
Если вы не хотите, чтобы ваш код зависел от платформы, вы можете использовать более переносимые решения - Библиотека Boost File System или std :: fstream .
Пример использования Boost ( более полный пример ):
copy_file (source_path, destination_path, copy_option::overwrite_if_exists);
Пример использования C ++ std :: fstream :
ifstream f1 ("input.txt", fstream::binary);
ofstream f2 ("output.txt", fstream::trunc|fstream::binary);
f2 << f1.rdbuf ();