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); } 

So as you have seen, it's quite a simple program. But I just can't find hello.txt in ../ My aim is to see what happens if I put a whatever number, says 10, instead of st_size which may be hundreds of bytes.

Edit:

Thanks for your answers. Well, I followed your advice and changed

 fd2=open("../",O_RDWR); 

to

 fd2=open("../hello.txt",O_RDWR); 

Also, I checked the return value of fstat and sendfile, everything is ok.

But the problem is still the same.

4
  • have you checked that hello.txt is excites in previous folder.? Commented Oct 5, 2011 at 10:37
  • Hi @Mr.32 , did you mean exists? Well, the results shows that hello.txt won't be created in the previous folder. Commented Oct 5, 2011 at 11:37
  • in previous folder there should be hello.txt which you are going to open.. Commented Oct 5, 2011 at 11:40
  • well, I've also tried to creat a hello.txt manually or by just using O_RDWR|O_CREAT. So I can find the file there but it's just an empty file. Commented Oct 5, 2011 at 13:20

3 Answers 3

2

You need to specify the filename in the second open, not just the directory name.

Please be sure to check the return values of all these functions, including fstat.

Sign up to request clarification or add additional context in comments.

Comments

2

Have you tried fd2 = open("../hello.txt",O_RDWR);?

1 Comment

Hi @CharlesB, I've followed your advice but the result is still the same
0

1>

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

replace with

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

2>

 fstat(fd1, &stat_buf); 

will fill up some info related to fd1 file in stat_buf . Here size of that file is also return in that structure with st_size element.

now in

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

total stat_buf.st_size bytes are going to send on fd2 file. if here if you write 10 then only 10 bytes will go in fd2.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.