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

Using Functions for Advanced File I/O Operations

LabVIEW 2011 Help

Edition Date: June 2011

Part Number: 371361H-01

»View Product Info

The File I/O palette includes functions to control each file I/O operation individually. Use these functions to create or open a file, read data from or write data to the file, and close the file. You also can use them to perform the following tasks:

  • Create directories.
  • Move, copy, or delete files.
  • List directory contents.
  • Change file characteristics.
  • Manipulate paths.

A path, shown as follows, is a LabVIEW data type that identifies the location of a file on disk.

The path describes the volume that contains the file, the directories between the top-level of the file system and the file, and the name of the file. Enter or display a path using the standard syntax for a given platform with the path control or indicator.

The following block diagram shows how to use File I/O functions to send numeric data to a tab-delimited spreadsheet file. When you run this VI, the Open/Create/Replace File function opens the numbers.xls file. The Write to Text File function writes the string of numbers to the file. The Close File function closes the file. If you do not close the file, the file stays in memory and is not accessible from other applications or to other users.

Compare the previous block diagram to the Write To Spreadsheet File VI, which completes the same task. The previous block diagram uses individual functions for each file operation, including using the Array To Spreadsheet String function to format the array of numbers as a string. The Write To Spreadsheet File VI completes multiple file operations, including opening the file, converting the array of numbers to a string, and closing the file.

Refer to the Write Datalog File Example VI in the labview\examples\file\datalog.llb for an example of using File I/O VIs and functions for advanced operations.

 

Using Path Controls and Refnums

In the previous example, the Open/Create/Replace File function returns a refnum, which is an identifier LabVIEW uses as a reference to a file. File I/O functions that accept path or refnum data types as inputs include an input with the phrase "(use dialog)", such as the path (use dialog) input. If you do not wire a control or constant to this input, a dialog box appears prompting users to select a file or directory when the function executes. Each of these functions include a cancelled output. If users click the Cancel button in the dialog box, cancelled returns TRUE and the function returns an error. If you wire a path data type to a File I/O function, the function opens with the minimum permissions needed to perform the function and eliminates the need for explicitly opening or closing the file.

For example, if you wire a path data type to the Read from Text File function, the function opens the file as read-only. LabVIEW closes the file if you do not wire the refnum out output to another function. If you wire a path to the Write to Text File function, the function opens the file as read/write and appends any text you write to the file after the last line of the file. In both cases, the functions close the file if you do not wire the refnum out output to another function. If you wire the refnum out output to another function, the file remains in memory until you explicitly close the file with the Close File function, such as in disk-streaming, or until another function or VI later in the data flow closes the reference.

Disk Streaming

You also can use File I/O functions for disk streaming operations, which save memory resources by reducing the number of times the function interacts with the operating system to open and close the file. Disk streaming is a technique for keeping files open while you perform multiple write operations, for example, within a loop.

Wiring a path control or a constant to the Write to Text File function, the Write to Binary File function, or the Write To Spreadsheet File VI adds the overhead of opening and closing the file each time the function or VI executes. VIs can be more efficient if you avoid opening and closing the same files frequently.

To create a typical disk-streaming operation, place the Open/Create/Replace File function before a loop, the read or write function in the loop, and the Close File function after the loop so continuous writing to a file can occur within the loop without the overhead associated with opening and closing the file in each iteration.

Disk streaming is ideal in lengthy data acquisition operations where speed is critical. You can write data continuously to a file while acquisition is still in progress. For best results, avoid running other VIs and functions, such as Analysis VIs and functions, until you complete the acquisition.

File Buffering

During each file I/O operation, LabVIEW calls the operating system (OS) and requests a data transfer to and from a file on a disk. Sending data to or receiving data from a disk may take a few milliseconds to complete. These milliseconds can accumulate because LabVIEW must call the OS each time you write a line to a file or request to read a file. To avoid these timely repetitions, most OS file systems provide a buffer to temporarily hold each piece of data waiting to be read or written to a file. When the buffer is full, the OS performs a single file I/O operation. This technique is known as buffering and LabVIEW enables it by default. Buffering reduces the number of times the OS must access the disk and expend processing time.

Sometimes, you can achieve faster data streaming rates if you disable buffering. For example, you may use a Redundant Array of Independent Disks (RAID) to increase processing speed. A RAID is a set of hard disks acting as a single disk that the OS can simultaneously access, taking less time to read or write data. If you access a RAID with buffering enabled, LabVIEW can take more time to copy data to the OS than the OS takes to actually write the data to the disk. You can disable buffering with the disable buffering input of the Open/Create/Replace File function to avoid these data copies and force the OS to send data directly to disk.

However, if you disable buffering, you must make sure the following conditions are true:

  • You must make the size of the data in the file a multiple of the sector size, indicated in bytes, of the disk that contains or may eventually contain the file. A sector is a subdivision of disk space that stores a fixed amount of data, typically 512 bytes. Use the sector size (bytes) output of the Get Volume Info function to determine the sector size of the disk. For LabVIEW to save data to a disk, the data can span multiple sectors but must fill each sector completely. A 512-byte sector requires 512 bytes of data. If the data is not a multiple of the sector size, you must pad the data with filler data and delete the filler data before LabVIEW reads back the file.
  • The data in the file must be aligned to a multiple of the alignment the disk requires. LabVIEW aligns the data, and you cannot change that alignment. If the data does not meet the alignment requirement, LabVIEW returns an error and you must enable buffering and reopen the file.

 

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