8

尝试使用这些函数复制文件,一切正常,直到程序遇到 memcpy 函数,该函数给出总线错误并终止进程。

void copy_mmap(char* in, char* out){

int input_fd, output_fd;

input_fd = open (in, O_RDONLY);
if (input_fd == -1) {
        printf("Error opening input file.\n");
        exit(2);
}

output_fd = open(out, O_RDWR | O_CREAT, S_IWUSR | S_IRUSR);
if(output_fd == -1){
    printf("Error opening output file.\n");
    exit(3);
}

struct stat st;
fstat(input_fd, &st);

char* target;
target=mmap(0, st.st_size+1, PROT_READ, MAP_SHARED, input_fd, 0);
if (target==(void*) -1){
    printf("Error mapping target with errno: %d.\n", errno);
    exit(6);
}


char* destination;
destination=mmap(0, st.st_size+1, PROT_READ | PROT_WRITE, MAP_SHARED, output_fd, 0);
if (destination==(void*) -1){
    printf("Error mapping destination with errno: %d.\n", errno);
    exit(5);
}

memcpy(destination, target, st.st_size);
munmap(destination, st.st_size);



}

未能找出问题所在,因为“总线错误”不是描述性错误消息,并且互联网上没有太多关于此问题的材料。

4

3 回答 3

22

当您将目标文件创建为新文件时,其大小为 0 字节。memcpy崩溃,因为它试图将数据写入文件末尾之外。

您可以通过将目标文件的大小预先调整为源文件的大小(使用ftruncate())来完成mmap()这项工作。

此外,您应该st.st_size作为第二个参数传递给mmap,而不是st.st_size+1st.st_size+1尝试映射大于文件大小的范围,这是无效的。

于 2013-01-09T17:02:23.060 回答
1

您也可以 lseek 到 off_t fileposition= lseek(output_fd, st.st_size-1, SEEK_SET) 并将一个空字符写入 output_fd。

于 2016-02-17T09:27:43.467 回答
0

也许尝试使用 memmove?您不是从同一个内存/文件/设备位置读取和写入吗?Memcpy 在这种情况下会失败。

于 2013-01-09T16:58:11.393 回答