Academic Company Events Community Support Solutions Products & Services Contact NI MyNI
39 ratings:
 2.25 out of 5     Rate this Document

How Can I Write and Read Binary Files Using LabVIEW?

Primary Software: LabVIEW Development Systems>>LabVIEW Full Development System
Primary Software Version: 7.1
Primary Software Fixed Version: N/A
Secondary Software: N/A

I would like to read and write binary files using LabVIEW. I would also like to understand how the procedure works so I can write and read data structures such as two-dimensional arrays to a binary file. How can I do this?

You can write and read from a binary file using the Write to Binary File and Read from Binary File VIs located in the Programming»File I/O functions palette. The Write to Binary File VI will also create a new binary file if the input file path does not yet exist. 

When reading a binary file, you must know the format the data was in when it was written to the file. Take careful notes of the format in which you store the numbers so that you will know how to read them later. Furthermore, there is no universally accepted format for binary files (unlike ASCII) so other applications or operating systems may be unable to read the file.

To extract the data in a binary format, you often need a header in the file to describe its contents. For example, if you have data in a file that contains a 2D array, the number of rows and columns should be included in the header to successfully extract the array.

Creating a binary file with a header can be done in two ways:

  1. A header is explicitly written to the file. LabVIEW requires an 8-byte header describing the number of rows and columns in a 2D array. For a 2D array, the header is created using the Array Size function.
  2. LabVIEW creates the header that it needs automatically. To generate the header automatically, a TRUE constant must be wired to Prepend Array or String Size? terminal of the Write to Binary File function.
In general, it is recommended to let the Write File function create the data header automatically, because the header varies according to the type of data saved to disk. However, there may be times where a user explicitly needs to know the header structure and write to (or read from) the disk. In this case, the Online Reference can be used to determine the necessary headers for different data types.

To read the binary file, you need to wire a dummy data type representing the data in the file into the Data Type input. For example, if we previously wrote a 2D array of double precision numbers to a file, in our read program we would have to wire a double precision number to the Data Type input to match the data type. LabVIEW will then assume that the appropriate header for the dummy data type is stored in the file, and uses it to extract the binary information.

To easily save data to disk in binary format:
  1. Write the data to disk, setting the Prepend Array or String Size? input on Write to Binary File to TRUE.
  2. When reading the data, wire a constant, control, or indicator of the appropriate data type to the Data Type input.

LabVIEW versions 7.1 and later ship with examples of both reading and writing binary files, which can be found using Example Finder (Help»Find Examples...). The files are named Read Binary and Write Binary and can be found by browsing to Fundamentals»File Input and Output directory in Example Finder, or by doing a search for Binary.

Related Links:
KnowledgeBase 10U39NAQ: Writing Binary Files with LabVIEW That Can Be Read by Other Applications
KnowledgeBase 1XU8RQPH: What Does the Term "Endian" Mean?
KnowledgeBase 2MKB41DV: How Can I Read the Binary Representation of a Floating Point Number in LabVIEW?


Report Date: 07/02/2002
Last Updated: 07/08/2014
Document ID: 2N1DMKM5

Your Feedback! poor Poor  |  Excellent excellent   Yes No
 Document Quality? 
 Answered Your Question? 
  1 2 3 4 5
Please Contact NI for all product and support inquiries.submit