私はファイル1.txt
2.txt
を作成し、1.txt
に内容を書きます。
次に、以下のコードを使用してコンテンツを2.txt
にコピーします。
しかし、動作しません。 2.txt
には何もありません。sendfileはファイルの内容をコピーしません
私の間違いを説明できますか?あなたが書きたい2つ目は、あなたから読み込みたいファイルディスクリプタであるに
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++のストリームを使用しています。これをしないでください。 – unwind
C++に移行しました。 ;) –
私はLinuxのC APIを使っているので、 "sendfile"なので、 "C"を作成しました。そして私はこれに注意を払うでしょう。ありがとう! – Tengchao