- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我创建文件 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;
}
最佳答案
根据 man , 签名是
ssize_t sendfile(int out_fd, int in_fd, off_t *offset, size_t count);
因此,第一个参数是要写入的文件描述符,第二个参数是要从中读取的文件描述符。
所以,你的电话应该是:
ssize_t size = sendfile(fd2,fd1,0,stat_buf.st_size);
关于c++ - sendfile 不复制文件内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14054732/
在我的 router/index.js 中,我使用 res.sendfile(..) 如下: var express = require('express'); var router = expres
设置路径的正确方法是什么? 在我的应用程序中,我使用此代码设置发送文件的路径。 app.get('/',function(req, res){//get,put,post,delete res
在我的程序中,我需要检查非阻塞套接字中 sendfile() 操作的完成情况。怎么办? 查了资料,上网查了下,没找到方法 最佳答案 它的工作方式与 send() 非常相似:如果套接字设置为 O_NON
这是我的客户端代码片段: while(((uint32_t)total_bytes_read != fsize) && ((nread = read(sockd, filebuffer, sizeof
这是 LIST ftp 的 cmd 的片段: count = file_list("./", &files); if((fp_list = fopen("listfiles.txt", "w")) =
我正在尝试重新发送一个 Json 文件,但是当我这样做时,它会不断返回许多特殊字符,如下所示: book����mark����8���8���`Q�����������eǾJu¼A����rs/f
我创建文件 1.txt 2.txt 并将一些内容写入 1.txt。 然后我使用下面的代码,想把内容复制到2.txt. 但它不起作用。 2.txt 中没有任何内容。 你能解释一下我的错误吗? int m
我正在尝试实现一个基本的文件服务器。我一直在尝试使用此处的 sendfile 命令:http://linux.die.net/man/2/sendfile我正在使用 TCP。 我可以让它正常发送,但它
我正在使用Wildcard Subdomains在我的 Express 服务器上,这样当用户访问 name.example.com 或从该子域的 API 请求某些内容时,一切都会正常工作。 (导航到
使用res.sendFile()时,响应中不会收到set-cookie header 。 app.get(sessionTracker, (req, res, next) => { res.coo
前奏 sendfile() 是一个非常有用的系统调用,原因有两个: 首先,它比 read()/write() (或 recv())代码更少/send()(如果您更喜欢 jive)循环。 其次,它比上述
服务器端 我尝试从 NodeJs 发送文件 /** * Exports data to PDF format route. */ app.post('/export/pdf', upload.si
我正在尝试使用 Linux 系统调用 sendfile() 来使用线程复制文件。 我有兴趣优化代码的这些部分: fseek(fin, size * (number) / MAX_THREADS, SE
作为服务器,我想从非阻塞客户端套接字读取数据,然后写入文件。 根据 sendfile 的手册页,如果 errno 设置为 EAGAIN,那么这仅表明,如果 output 文件描述符设置为非阻塞,则对
我正在尝试向客户端发送静态 html。 我使用这种方法成功地做到了这一点 app.get('/forgot', function (req, res) { console.log("F
我的/blog 和/post 在浏览器控制台中触发了许多 404 未找到错误,可以在 here 中找到这些错误即使 blog.html 和 post.html 如果我从文件资源管理器打开它都可以工作!
我正在做一个 Node 项目,在其中公开我的公共(public)文件夹,例如: app.use(express.static(path.join(__dirname, '/public'))) 现在,
我正在尝试使用 sendfile() 来实现一个复制程序。 但是当我尝试复制目录时它失败了。目录在 Linux 中不是一种特殊的文件类型吗? 这是我现在使用的代码。它是从 StackOverflow
假设我的 Express.js 应用程序中有这个路由处理程序: app.get('/files/:name', function (req, res) { res.sendfile('path
我正在尝试在 Linux 2.6.32 下测试 sendfile() 系统调用以在两个常规文件之间进行零拷贝数据。据我了解,它应该可以工作:从 2.6.22 开始,sendfile() 已使用 spl
我是一名优秀的程序员,十分优秀!