If you store arrays of numeric data in a file, you might find it necessary to access data at random locations in the file. You cannot randomly access data stored in text files when the data contains negative signs, varying number of digits in individual data points, and other factors. For example, if you have an array of 100 numbers ranging in value from 0 to 1,000, you cannot predict where a given element in the array is located within the text file. The problem is that in text, the number 345 requires three bytes of storage, while the number 2 requires only one byte. Therefore, you cannot predict the location of an arbitrary array element in the file.
Such obstacles do not occur in binary files. In a binary file, the flattened format of a number in LabVIEW is a binary image of the number itself.
Therefore, each number in the array uses a fixed number of bytes of storage on disk. If you know that a file stores single-precision numbers, which use four bytes per number, you can read an arbitrary group of elements from the array, as shown in the following example.
In the previous block diagram example, the pos mode input of the Read File function is set tostart, which means that the function begins reading data at pos offset bytes from the beginning of the file. The first byte in the file has an offset of zero. So, the location of the nthelement in an array of single-precision floating-point numbers stored in this file is 4×n bytes from the beginning of the file. A single-precision floating-point constant, wired to the byte stream type input, tells the Read File function to read
single-precision floating-point values from the file. The # values to read control, connected to the count input of the Read File function, tells the function how many single-precision elements to read from the file. Notice that when the count input is wired, LabVIEW places the output data in an array, because LabVIEW reads more than one value from the file.
The following points are important to remember about random access operations:
• When performing text and binary file I/O, remember that values for the pos offset terminal are measured in bytes.
• The count input in the Read File function controls how many bytes of information are read from the file when the byte stream type input is unwired. The data read from the file is returned in a string.
• If the byte stream type input is wired, the data output of the Read File function is of the same data type as the byte stream type input when the count input is unwired.
• If the byte stream type input is wired and the count input has data connected to it, then the Read File function returns an array containing count elements of the same data type as the byte stream type input.
• Refer to the LabVIEW Help and the LabVIEW User Manual for more
Exercise 4-1 Binary File Writer VI
Objective: To build a VI that writes data to a binary file with a simple data formatting scheme.
This VI saves data to a binary file using a simple formatting scheme in which the file’s header is a long word integer (I32) containing the number of data points in the file. In the next exercise, build a VI that reads the binary file.
Front Panel
1. Open a new VI.
2. Build the following front panel. When you create the menu ring, recall that a shortcut for adding a new item to the list of options is to press
<Shift-Enter> after entering the text for an item in the list. EnterZero foritem 0 in the list,Sine for item one, and so on. To view the numeric value of the menu ring, right-click the control and select Visible Items»Digital Display.
Macintosh, <Shift-Return>; Sun, <Shift-Return>; HP-UX, <Shift-Enter>; and Linux, <Shift-Enter>
Note The appearance of the menu ring control varies from one platform to the next.
3. Open the block diagram.
Block Diagram
4. Build the following block diagram.
a. Place the Open/Create/Replace File VI located on the
Functions»File I/O palette on the block diagram. This VI creates or replaces a file.
b. Place the Write File function located on the Functions»File I/O palette on the block diagram. This function appears twice in this exercise. The first function writes the binary file’s header
information, which is a four-byte integer containing the number of values written to the file. The second instance writes the array of data to the file.
c. Place the Close File function located on the Functions»File I/O palette on the block diagram. In this exercise, this function closes the binary file after data has been written to it.
d. Place the Simple Error Handler VI located on the Functions»Time
& Dialog palette on the block diagram. In the event of an error, this VI displays a dialog box with information about the error and where it occurred.
e. To create this constant, right-click the pos mode input of the Write File function and select Create»Constant. Set the position mode to startto ensure that new data are written relative to the beginning of the file.
f. To create this constant, right-click the function input of the Open/Create/Replace File VI and select Create»Constant. By selecting create or replace, you allow the user to create a new file or overwrite an existing file.
g. Place the Acquire Signal VI located on the Functions»User Libraries»Basics-II Course palette on the block diagram. In this exercise, this VI generates the waveform selected by the Select
h. Place the Array Size function located on the Functions»Array palette on the block diagram. In this exercise, this function returns the number of elements in the 1D array of data to be written to the file.
5. Save the VI asBinary File Writer.vi. Run it, saving data to the file on disk nameddata.bin.