Linux C++ FileIO Copy -VS- System("cp file1.x file2.x)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3680730/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 21:37:22  来源:igfitidea点击:

C++ FileIO Copy -VS- System("cp file1.x file2.x)

c++linuxg++filesystems

提问by MichaelICE

Would it be quicker/efficient to write a file copy routine or should I just execute a System call to cp?

编写文件复制例程会更快/更有效还是我应该只执行对cp的系统调用?

(The file system could differ [nfs, local, reiser, etc], however it would always be on a CentOS linux system)

(文件系统可能不同 [nfs、local、reiser 等],但它总是在 CentOS linux 系统上)

采纳答案by Alexander Rafferty

Invokinga shellby using system ()function is not efficient and not very secure.

调用一个通过使用系统()函数的效率不高,而不是非常安全。

The most efficient way to copy a file in Linux is to use sendfile ()system call. On Windows, CopyFile ()API function or one of its related variants should be used.

在 Linux 中复制文件最有效的方法是使用sendfile()系统调用。在 Windows 上,应使用CopyFile ()API 函数或其相关变体之一。

Exampleusing sendfile:

使用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;
}

If you do not want your code to be platform dependent, you may stick with more portable solutions - Boost File System libraryor std::fstream.

如果您不希望您的代码依赖于平台,您可以坚持使用更便携的解决方案 - Boost File System librarystd::fstream

Example using Boost (more complete example):

使用 Boost 的示例(更完整的示例):

copy_file (source_path, destination_path, copy_option::overwrite_if_exists);

Example using C++ std::fstream:

使用 C++ std::fstream 的示例:

ifstream f1 ("input.txt", fstream::binary);
ofstream f2 ("output.txt", fstream::trunc|fstream::binary);
f2 << f1.rdbuf ();

回答by Eugene Mayevski 'Callback

With your own routine you can control the size of the block used for copying, which you can't do with cp. Also, you can spawn different threads for reading and writing data (to further speed up process). Finally, spawning an external process takes extra time (important if you copy small files).

使用您自己的例程,您可以控制用于复制的块的大小,而 cp 则无法做到这一点。此外,您可以生成不同的线程来读取和写入数据(以进一步加快进程)。最后,生成外部进程需要额外的时间(如果您复制小文件,这一点很重要)。

回答by dash-tom-bang

It would not be time efficient to write a file copy routine.

编写文件复制例程不会节省时间。

It is resource intensive to call system to shell a cp.

调用 system 来 shell cp 是资源密集型的。

You'll be far better served by figuring out the system (function) call that you can make to copy the file. E.g. on Windows it's just CopyFile(src, dst)if I recall correctly.

通过找出可以用来复制文件的系统(函数)调用,您会得到更好的服务。例如在 Windows 上,CopyFile(src, dst)如果我没记错的话。

回答by M. Sadeq H. E.

C++ File IO is more portable and more low-level, so it is more flexible.

C++ File IO更便携,更底层,所以更灵活。

回答by Alexander Rafferty

I would put my money on that the OS knows the most efficient way to copy file A to file B. The same applies for any api functions.

我会把钱花在操作系统知道将文件 A 复制到文件 B 的最有效方法上。这同样适用于任何 api 函数。