File Sink

Used to write a stream to a binary file.

This file can be read into any programming environment that can read binary files (MATLAB, C, Python, ...). It can also be played back in GRC using a File Source. For example, if complex type is chosen, then the binary file will be full of float32s in IQIQIQ order. There is no meta data or anything else included with the binary data. For more information on handling this data, see the Handling File Sink data section below.

Parameters
(R): Run-time adjustable


 * File (R)
 * Path of the file to open and write output to. If the specified file name does not exist at that location, it creates a file of that name over there. Otherwise, if the file already exists, it may overwrite or append the file based on the append option.


 * Unbuffered
 * Specifies whether the output is buffered in memory. If the output is unbuffered, the data will be flushed to the file each time the work function is called. This can cause the flowgraph to run slow due to the time required to access the disk each time.


 * Append File
 * Gives an option to either append to the file or to overwrite the file.

Example Flowgraph
This flowgraph shows a File Sink which outputs text to the terminal (/dev/stdout).



What is the file format of a file_sink? How can I read files produced by a file sink?
All files written by File Sink are in pure binary format with no metadata. A floating point data stream is saved as 32 bits in the file, one after the other. A complex signal has 32 bits for the real part and 32 bits for the imaginary part. Reading back a complex number means reading in 32 bits, saving that to the real part of a complex data structure, and then reading in the next 32 bits as the imaginary part of the data structure. And just keep reading the data.

The exception to the format is when using the metadata file format. These files are produced by the File Meta Sink block and read by the File Meta Source block. See the manual page on the Metadata Information for more information about how to deal with these files.

Reading from Python
A one-line Python command to read the entire file into a numpy array is:

Where dtype is one of,  ,  ,   or whatever type you were using.

Reading from Octave / Matlab
If you're using octave (or are stuck with using Matlab, our condolences), the following is a method to read all floats written with a File Sink to a variable:

Replace  with ,  or   as appropriate. Use  to convert interleaved real, imaginary values to an array of complex values.

Reading from plain C
This is the easiest, since we assume you know how `fread` works if you're writing C, and how to cast a pointer:

Furthermore, here's a way of simply mapping your file to memory and use that to access the values, directly. This is sensible if you do not intend to load the full file into RAM, but want to jump around in it.

Reading from C++
There's many ways to do this in C++. In this example, we're reading a file, either at once into a large buffer, or in chunks of limited size, until we're done. We're using  as our means of reading the file. This example is rather complete, and illustrates a lot of error handling, file checking, as well as chunk-wise reading etc. It also calculates instantaneous powers, and it outputs these. Your own implementation might be shorter!

Source Files

 * C++ files
 * file_sink_impl.cc


 * Header files
 * file_sink_impl.h


 * Public header files
 * file_sink.h


 * Block definition
 * blocks_file_sink.block.yml