我创建文件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;
}发布于 2012-12-27 20:30:37
根据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);
发布于 2012-12-27 20:31:51
根据sendfile原型,您想要写入的fd应该是第一个参数,从中读取的fd应该是第二个参数。但是,你却以完全相反的方式使用它。
因此,您的sendfile语句应如下所示:
ssize_t size = sendfile(fd2,fd1,0,stat_buf.st_size);https://stackoverflow.com/questions/14054732
复制相似问题