Ç流:复制数据从一个流至直接另一个,而不使用缓冲 [英] C streams: Copy data from one stream to another directly, without using a buffer

查看:182
本文介绍了Ç流:复制数据从一个流至直接另一个,而不使用缓冲的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

欲数据从一个流复制到另一个。现在通常情况下,我会做这样的:

I want to copy data from one stream to another. Now normally, I would do it this way:

n = fread(buffer, 1, bufsize, fin);
fwrite(buffer, 1, n, fout);

有没有直接将数据从 FOUT ,而无需通过缓冲,即去的方法而不是装订> buffer-> FOUT ,我想直接做装订> FOUT (无缓冲区)。

Is there a way to write the data directly from fin to fout, without going through a buffer, i.e. instead of fin->buffer->fout, I want to directly do fin->fout (no buffer).

是否有可能在ANSI C这样做呢?如果没有,是否有可能与POSIX功能做呢?或特定的Linux解决方案?

Is it possible to do so in ANSI C? If not, is it possible to do it with POSIX functions? Or a Linux-specific solution?

推荐答案

2只可能的Linux解决方案的拼接()的sendfile( )。他们所做的事情是没有它复制数据离开过内核空间,从而使潜在的显著性能优化。

2 possible Linux-only solutions are splice() and sendfile(). What they do is copy data without it ever leaving kernel space, thus making a potentially significant performance optimization.

注意都有局限性


  • 的sendfile()需要一个插座其输出为Linux内核2.6.33之前,在这之后,任何文件都可以输出,同时它要求输入,支持的mmap()操作,这意味着输入可'T是标准输入或管道。

  • sendfile() requires a socket for its output for Linux kernels before 2.6.33, after that, any file can be the output, and also it requires the input to support mmap() operations, meaning the input can't be stdin or a pipe.

拼接()要求之一输入或输出流是一个管(不知道两者),并且还为内核版本2.6.30.10和老年人,它需要是不支持剪接的管流中的文件系统。

splice() requires one of the input or output streams to be a pipe (not sure about both), and also for kernel versions 2.6.30.10 and older, it requires the file system for the stream that is not a pipe to support splicing.

编辑:注意一些文件系统可能不支持拼接为Linux 2.6.30.10及以下

这篇关于Ç流:复制数据从一个流至直接另一个,而不使用缓冲的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆