0
#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 の戻り値を確認しましたが、すべて問題ありません。

しかし、問題は同じです。

4

3 に答える 3

2

openディレクトリ名だけでなく、2 番目の でファイル名を指定する必要があります。

を含むこれらすべての関数の戻り値を必ず確認してくださいfstat

于 2011-10-05T09:26:42.563 に答える
2

試しましたfd2 = open("../hello.txt",O_RDWR);か?

于 2011-10-05T09:26:53.717 に答える
0

1>

  fd1=open("./hello.txt",O_RDONLY); //read only
  fd2=open("../",O_RDWR);           //both read and write

と置換する

 fd1=open("../hello.txt",O_RDONLY); //read only
  fd2=open("../your_file_name",O_RDWR);         

2>

 fstat(fd1, &stat_buf); 

stat_buf の fd1 ファイルに関連する情報を埋めます。ここで、そのファイルのサイズも st_size 要素を持つその構造体に返されます。

 rc=sendfile (fd2, fd1, &offset, stat_buf.st_size);

合計 stat_buf.st_size バイトが fd2 ファイルで送信されます。ここで 10 を書き込むと、fd2 には 10 バイトしか入りません。

于 2011-10-05T09:35:41.437 に答える