小编Jam*_*ell的帖子

为什么mmap()失败并且文件复制程序的目标文件被拒绝?

我想通过在Linux中使用内存映射I/O来尝试将文件的内容复制到另一个文件mmap().目的是自己检查一下这是否比使用更好fread(),fwrite()以及它如何处理大文件(例如,像几个GiB,因为文件被读取整个我想知道如果我需要有这么多的内存量).

这是我正在使用的代码:

// Open original file descriptor:
int orig_fd = open(argv[1], O_RDONLY);
// Check if it was really opened:
if (orig_fd == -1) {
    fprintf(stderr, "ERROR: File %s couldn't be opened:\n", argv[1]);
    fprintf(stderr, "%d - %s\n", errno, strerror(errno));
    exit(EX_NOINPUT);
}
// Idem for the destination file:
int dest_fd = open(argv[2], O_WRONLY | O_CREAT | O_TRUNC, 0644);
// Check if it was really opened:
if (dest_fd == -1) {
    fprintf(stderr, "ERROR: File %s couldn't be …
Run Code Online (Sandbox Code Playgroud)

c linux file-io mmap copy

12
推荐指数
1
解决办法
2万
查看次数

标签 统计

c ×1

copy ×1

file-io ×1

linux ×1

mmap ×1