Academic Company Events Community Support Solutions Products & Services Contact NI MyNI
43 ratings:
 2.46 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: 2010
Primary Software Fixed Version: N/A
Secondary Software: N/A

Problem:
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?

Solution:
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»Read Binary File.vi and the Write Binary File.vi that demonstrate how to use these functions.

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.

When writing integers to a binary file the bits of the integer are mapped to bits in the file. For example if an I32 is written to a binary file it takes 32 bits of space.

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:

  1. Manually: The Array Size function can be used to determine these values. A FALSE constant must be wired to the Prepend Array or String Size? terminal of the Write to Binary File function. 
  2. Automatically: To generate the header automatically, a TRUE constant can be wired to Prepend Array or String Size? terminal of the Write to Binary File function (but is not required).
In general, it is recommended to let the Write to Binary 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. 

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:




Related Links:
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?

Attachments:
Read and Write Binary File Examples.vi




Report Date: 07/02/2002
Last Updated: 12/18/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