Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/140.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ sendfile没有';不复制文件内容_C++_Linux_System Calls_Sendfile - Fatal编程技术网

C++ sendfile没有';不复制文件内容

C++ sendfile没有';不复制文件内容,c++,linux,system-calls,sendfile,C++,Linux,System Calls,Sendfile,我创建文件1.txt2.txt,并将一些内容写入1.txt 然后我使用下面的代码,希望将内容复制到2.txt 但它不起作用。2.txt中没有任何内容 你能解释我的错误吗 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 s

我创建文件
1.txt
2.txt
,并将一些内容写入
1.txt

然后我使用下面的代码,希望将内容复制到
2.txt

但它不起作用。
2.txt中没有任何内容

你能解释我的错误吗

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;
}
intmain()
{
int fd1=打开(“1.txt”,O_RDWR);
int fd2=打开(“2.txt”,O_RDWR);
结构统计;
fstat(fd1和stat_buf);
ssize_t size=sendfile(fd1、fd2、0、stat_buf.st_size);
cout根据,签名为

ssize\u t sendfile(int out\u fd、int in\u fd、off\u t*偏移量、大小\u t计数);

因此,第一个参数是要写入的文件描述符,第二个参数是要读取的文件描述符

因此,您的电话应该是:

ssize\u t size=sendfile(fd2,fd1,0,stat\u buf.st\u size);

根据原型,您要写入的fd应该是第一个参数,从中读取的fd应该是第二个参数。但是,您使用它的方式正好相反

因此,sendfile语句应如下所示:

ssize_t size = sendfile(fd2,fd1,0,stat_buf.st_size);

这是标记的“C”,但很明显使用C++流。不要这样做。因为我使用Linux C API——“sEngFr档”,所以我称为“C”。。我会注意这一点。谢谢!在
sendfile
中,你应该颠倒
fd2
fd1
的顺序。如果你使用有意义的变量名称,会更清楚。例如,在_文件中,在_文件中会更容易发现它们是错误的。