How Can I Write and Read Binary Files Using LabVIEW?
Primary Software: LabVIEW Development Systems>>LabVIEW Full Development System
Primary Software Version: 2010
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?
LabVIEW programs can access binary files with the Write to Binary File and Read from Binary File VIs located in the Programming»File I/O functions palette. There are several examples in the LabVIEW Example Finder (Help»Find Examples...) under Fundamentals»File Input and Output»
Accessing binary files follows the same code structure as accessing other files. Specifically the user opens the file, calls the read and write functions as needed, and closes the file afterwards. The image below shows a simple case of how one would write a 32-bit signed integer to a binary file:
When reading a binary file, the user must know the format the data was in when it was written to the file. When writing binary files take careful notes of the format in which the data is stored so that it can be properly read back later. It is up to the user to choose the structure of their binary file and there is no universally accepted format for binary files (unlike ASCII) so other applications or operating systems may be unable to read meaningful data from the file.
Writing an I32 2D Array to a binary file is more complex because the array may have different numbers of rows and columns. To simplify this 2D Array data is often saved with a header that describes these dimensions. To read a 2D array from a binary file LabVIEW requires an 8-byte header. Creating this header can be done in two ways:
The image below shows a simple case of writing a 2D array to a binary file and automatically creating the header.
To read the binary file, wire the expected data type into the Data Type input. For example, if the file contains a 2D array of 32 bit integers connect a 2D Array of 32 bit integers to the Data Type input. 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. The image below shows a simple case of reading 2D data from a binary file:
KnowledgeBase 10U39NAQ: Writing Binary Files with LabVIEW That Can Be Read by Other Applications
KnowledgeBase 2MKB41DV: How Can I Read the Binary Representation of a Floating Point Number in LabVIEW?
Example Program: Reading Little Endian Binary Files with LabVIEW 7.1
Example Program: How LabVIEW Stores SGL Floating-Point Numbers in Memory (IEEE 754 Format)
Read and Write Binary File Examples.vi
Report Date: 07/02/2002
Last Updated: 12/18/2014
Document ID: 2N1DMKM5