Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

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).

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?

like image 399
Robby75 Avatar asked Jan 09 '14 08:01

Robby75


People also ask

Is file a buffered stream in C?

No, fopen() does not return a buffer, it returns a FILE pointer. FILE is an opaque structure.

What is stream copy?

CopyTo(Stream) Reads the bytes from the current stream and writes them to another stream. Both streams positions are advanced by the number of bytes copied. CopyTo(Stream, Int32) Reads the bytes from the current stream and writes them to another stream, using a specified buffer size.

What is stream function C?

A stream is a logical entity that represents a file or device, that can accept input or output. All input and output functions in standard C, operate on data streams. Streams can be divided into text, streams and binary streams.


1 Answers

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.

Note that both have limitations:

  • 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.

  • 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.

Edit: Note that some filesystems might not support splicing for Linux 2.6.30.10 and below.

like image 71
sashoalm Avatar answered Oct 02 '22 20:10

sashoalm