#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <sys/sendfile.h>
#include <sys/stat.h>
#include <fcntl.h>
int main(){
int fd1,fd2,rc;
off_t offset = 0;
struct stat stat_buf;
fd1=open("./hello.txt",O_RDONLY); //read only
fd2=open("../",O_RDWR); //both read and write
fstat(fd1, &stat_buf); //get the size of hello.txt
printf("file size: %d\n",(int)stat_buf.st_size);
rc=sendfile (fd2, fd1, &offset, stat_buf.st_size);
}
ご覧のとおり、非常に単純なプログラムです。しかし、../ で hello.txt を見つけることができません。私の目的は、数百バイトになる可能性がある st_size の代わりに、10 などの数値を入力するとどうなるかを確認することです。
編集:
回答ありがとうございます。さて、私はあなたのアドバイスに従い、変更しました
fd2=open("../",O_RDWR);
に
fd2=open("../hello.txt",O_RDWR);
また、fstat と sendfile の戻り値を確認しましたが、すべて問題ありません。
しかし、問題は同じです。