【发布时间】:2012-12-12 20:19:01
【问题描述】:
我创建文件1.txt2.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;
}
【问题讨论】:
-
这被标记为“c”,但显然使用的是 C++ 流。不要这样做。
-
因为我使用的是 linux C API --"sendfile",所以我标记了 "C"。我会注意这一点。谢谢!
标签: c++ linux system-calls sendfile