Invoking एक shellsystem() फ़ंक्शन का उपयोग करके कुशल और बहुत सुरक्षित नहीं नहीं है।
लिनक्स में फ़ाइल की प्रतिलिपि बनाने का सबसे प्रभावी तरीका sendfile() सिस्टम कॉल का उपयोग करना है। विंडोज़ पर, CopyFile() एपीआई फ़ंक्शन या इसके संबंधित संस्करणों में से एक का उपयोग किया जाना चाहिए।
Examplesendfile का उपयोग कर:
#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 library या std::fstream। बूस्ट
उदाहरण का उपयोग करते हुए (more complete example):
copy_file (source_path, destination_path, copy_option::overwrite_if_exists);
उदाहरण का उपयोग करते हुए सी ++ std::fstream:
ifstream f1 ("input.txt", fstream::binary);
ofstream f2 ("output.txt", fstream::trunc|fstream::binary);
f2 << f1.rdbuf();
की [एक, समझदार सुरक्षित और कुशल तरीके से एक फाइल कॉपी] (http
स्रोत
2010-09-09 21:47:49
संभव डुप्लिकेट: // stackoverflow.com/questions/10195343/copy-a-file-in-an-sane-safe-and- कुशल-way) –