Company Events Academic Community Support Solutions Products & Services Contact NI MyNI

Choosing a File I/O Format

LabVIEW 2010 Help

Edition Date: June 2010

Part Number: 371361G-01

»View Product Info
Download Help (Windows Only)

The VIs on the File I/O palette you use depend on the format of the files. You can read data from or write data to files in three formats—text, binary, and datalog. The format you use depends on the data you acquire or create and the applications that will access that data.

Use the following basic guidelines to determine which format to use:

  • If you want to make your data available to other applications, such as Microsoft Excel, use text files because they are the most common and the most portable.
  • If you need to perform random access file reads or writes or if speed and compact disk space are crucial, use binary files because they are more efficient than text files in disk space and in speed.
  • If you want to manipulate complex records of data or different data types in LabVIEW, use datalog files because they are the best way to store data if you intend to access the data only from LabVIEW and you need to store complex data structures.

When to Use Text Files

Use text format files for your data to make it available to other users or applications, if disk space and file I/O speed are not crucial, if you do not need to perform random access reads or writes, and if numeric precision is not important.

Text files are the easiest format to use and to share. Almost any computer can read from or write to a text file. A variety of text-based programs can read text-based files. Most instrument control applications use text strings.

Store data in text files when you want to access it from another application, such as a word processing or spreadsheet application. To store data in text format, use the String functions to convert all data to text strings. Text files can contain information of different data types.

Text files typically take up more memory than binary and datalog files if the data is not originally in text form, such as graph or chart data, because the ASCII representation of data usually is larger than the data itself. For example, you can store the number –123.4567 in 4 bytes as a single-precision floating-point number. However, its ASCII representation takes 9 bytes, one for each character.

In addition, it is difficult to randomly access numeric data in text files. Although each character in a string takes up exactly 1 byte of space, the space required to express a number as text typically is not fixed. To find the ninth number in a text file, LabVIEW must first read and convert the preceding eight numbers.

You might lose precision if you store numeric data in text files. Computers store numeric data as binary data, and typically you write numeric data to a text file in decimal notation. A loss of precision might occur when you write the data to the text file. Loss of precision is not an issue with binary files.

Use the File I/O VIs and functions to read from or write to text files and to read from or write to spreadsheet files.

Refer to the following VIs for examples of using file I/O with text files:

  • labview\examples\file\smplfile.llb  
  • labview\examples\file\sprdsht.llb  

When to Use Binary Files

Storing binary data, such as an integer, uses a fixed number of bytes on disk. For example, storing any number from 0 to 4 billion in binary format, such as 1, 1,000, or 1,000,000, takes up 4 bytes for each number.

Use binary files to save numeric data and to access specific numbers from a file or randomly access numbers from a file. Binary files are machine readable only, unlike text files, which are human readable. Binary files are the most compact and fastest format for storing data. You can use multiple data types in binary files, but it is uncommon.

Binary files are more efficient because they use less disk space and because you do not need to convert data to and from a text representation when you store and retrieve data. A binary file can represent 256 values in 1 byte of disk space. Often, binary files contain a byte-for-byte image of the data as it was stored in memory, except for cases like extended and complex numeric values. When the file contains a byte-for-byte image of the data as it was stored in memory, reading the file is faster because conversion is not necessary.

Note   Text and binary files are both known as byte stream files, which means they store data as a sequence of characters or bytes.

Use the File I/O VIs and functions to read from and write to binary files. Consider using the binary file functions if you want to read numeric data from or write numeric data to a file or if you want to create text files for use on multiple operating systems.

Refer to the following VIs for examples of reading and writing an array of double-precision floating-point values from and to a binary file, respectively:

  • Read Binary File VI: labview\examples\file\smplfile.llb  
  • Write Binary File VI: labview\examples\file\smplfile.llb  

When to Use Datalog Files

Use datalog files to access and manipulate data only in LabVIEW and to store complex data structures quickly and easily.

A datalog file stores data as a sequence of identically structured records, similar to a spreadsheet, where each row represents a record. Each record in a datalog file must have the same data types associated with it. LabVIEW writes each record to the file as a cluster containing the data to store. However, the components of a datalog record can be any data type, which you determine when you create the file.

For example, you can create a datalog whose record data type is a cluster of a string and a number. Then, each record of the datalog is a cluster of a string and a number. However, the first record could be (“abc”,1), while the second record could be (“xyz”,7).

You may eventually decide to change the record data type of your datalog file. In doing so, any VIs you created to manipulate those records will have to be updated to read the new data type. However, once you update your VIs, they cannot read any files created with the old record data type.

Using datalog files requires little manipulation, which makes writing and reading much faster. It also simplifies data retrieval because you can read the original blocks of data back as a record without having to read all records that precede it in the file. Random access is fast and easy with datalog files because all you need to access the record is the record number. LabVIEW sequentially assigns the record number to each record when it creates the datalog file.

You can access datalog files from the front panel and from the block diagram.

LabVIEW writes a record to a datalog file each time the associated VI runs. You cannot overwrite a record after LabVIEW writes it to a datalog file. When you read a datalog file, you can read one or more records at a time.

Another way to create a datalog file is by using front panel data logging, which records data for use in other VIs and in reports.

Refer to the labview\examples\file\datalog.llb for examples of reading and writing datalog files. 


Your Feedback! poor Poor  |  Excellent excellent   Yes No
 Document Quality? 
 Answered Your Question? 
Add Comments 1 2 3 4 5 submit