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:
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.
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.
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.
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: