gpt4 book ai didi

c++ - sendfile 不复制文件内容

转载 作者:IT王子 更新时间:2023-10-29 01:22:32 27 4
gpt4 key购买 nike

我创建文件 1.txt 2.txt 并将一些内容写入 1.txt
然后我使用下面的代码,想把内容复制到2.txt.
但它不起作用。 2.txt 中没有任何内容。

你能解释一下我的错误吗?

int main()
{
int fd1 = open("1.txt",O_RDWR);
int fd2 = open("2.txt",O_RDWR);
struct stat stat_buf ;
fstat(fd1,&stat_buf);
ssize_t size = sendfile(fd1,fd2,0,stat_buf.st_size);
cout<<"fd1 size:"<<stat_buf.st_size<<endl; //output 41
cout<<strerror(errno)<<endl; //output success

close(fd1);
close(fd2);
return 0;
}

最佳答案

根据 man , 签名是

ssize_t sendfile(int out_fd, int in_fd, off_t *offset, size_t count);

因此,第一个参数是要写入的文件描述符,第二个参数是要从中读取的文件描述符。

所以,你的电话应该是:

ssize_t size = sendfile(fd2,fd1,0,stat_buf.st_size);

关于c++ - sendfile 不复制文件内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14054732/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com