【发布时间】:2011-12-01 07:53:21
【问题描述】:
#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 我的目标是看看如果我输入一个任意数字会发生什么,比如 10,而不是可能是数百字节的 st_size。
编辑:
感谢您的回答。好吧,我听从了你的建议并改变了
fd2=open("../",O_RDWR);
到
fd2=open("../hello.txt",O_RDWR);
另外,我检查了fstat和sendfile的返回值,一切正常。
但问题还是一样。
【问题讨论】:
-
您是否检查过 hello.txt 在上一个文件夹中是否存在?
-
嗨@Mr.32,你的意思是存在吗?嗯,结果显示不会在上一个文件夹中创建hello.txt。
-
在上一个文件夹中应该有你要打开的 hello.txt..
-
好吧,我也尝试过手动或仅使用 O_RDWR|O_CREAT 创建一个 hello.txt。所以我可以在那里找到文件,但它只是一个空文件。