C-streams: copying data from one stream to another directly, without using a buffer

I want to copy data from one stream to another. Now I usually do this:

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

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

Can this be done in ANSI C? If not, is it possible to do this using POSIX functions? Or a solution for Linux?

+7
c linux stream posix
source share
1 answer

2 possible solutions for Linux are splice () and sendfile () . What they do is copy data without leaving the kernel space, thereby making a potentially significant performance optimization.

Please note that both have limitations :

  • sendfile () requires a socket for output for Linux kernels up to 2.6.33, after that any file can be output, and also requires input to support mmap() operations, which means that the input cannot be stdin or pipe.

  • splice () requires that one of the input or output streams be a channel (not sure of both), and kernel versions 2.6.30.10 and later require a file system for a stream that is not a channel for splicing support.

Edit: Note that some file systems may not support splicing for Linux 2.6.30.10 and below .

+6
source share

All Articles