In this tutorial you will learn how to read and write data from and to a text files (with .txt extension) in LabView. At the start you are provided with a simple and explanatory introduction of text files there uses and why do we need to read and write data on them. After that their uses and functionality in LabView is explained with the help of a VI, and an example block diagram in the explanation section. At the end of the tutorial you are provided with an exercise to do it by yourself, and in the next tutorials I will assume that you have done those exercises and I will not explain the concept regarding them.
Introduction to text files in labview
A computer file which is structured as a sequence of lines of electronic text is known as a text file also spelled as “textfile”; an old alternative name is “flatfile”. Data can be stored within a computer file with the help of a text file.
Easiest format to use and to share data are text files. Writing and reading to a text file can be done by using a computer. Almost every simple text based programs can read and write data to a text file. Data to be processed in text files in of type string.
In LabView, data from your devices can be stored permanently to a text file in simple file I/O function blocks. You can create a text file, open it, write data to a text file, read data from a file and after all this close a text file in LabView, as we will discuss shortly in this tutorial.
How to read and write to text files in labview
I will now explain the working of LabView when used with a text file. This will help you understand the main purpose of this tutorial in detail. Try to do all the exercise in the tutorial on your own to have a better understanding of what I am trying to explain. Create a new blank VI and save it for future use as we have been doing in every tutorial.
Writing data to a text file in labview
- From the control palette on the front panel select strings and paths and then select string control and place it on the front panel as shown in the figure below,
Figure 1: String control
- Extend its size and rename it according to the need as in this case I have named as “Data to be written to text file” and write the string or data inside the string control that you want to be written to you text file. In this case I have written “I want this data to be written to my text file named “Writing to text file””.
Figure 2: String to be written to text file
- From the function palette select File I/O and then select write to text file block as shown in the figure below,
Figure 3: Write to text file block placement
Figure 4: Write to text file block
- If you want to know about the pins present at the input or output sides of a block simply click on the question mark at the top right corner of the block diagram as shown in the figure below,
Figure 5: Help window
- A window will appear, after that hover over the block about which you want to know, and the details about that block will appear in the help window as shown in the figure below,
Figure 6: Pins description
- Connect the string control you created previously to the pin named as text in the above diagram and at the pin named as file(use dialog) in the above figure, create a control as we have done before, this will automatically create a file path control as shown in the figure below,
Figure 7: Path control
- We haven’t discussed these types of controls so far but in this tutorial you will learn how to use them too. On the front panel this path control will have a window and a browsing button as shown in the figure below,
Figure 8: Browsing button
- This button will allow you to browse the text file to which you want to write the data in the string you created previously as shown in the figure below,
Figure 9: Choosing the file
- Doing so will display the path of this file in your computer on the window of the ath control as shown in the figure below,
Figure 10: Path of the file
- Running this VI will write the data written in the string control to the text file the path of which you have specified. Open the text file and see if the data has written o it or not, refer to the figure below,
Figure 11: Data written
Reading data from a text file in labview
- Now if we want to read the same text file click right on the write to text file block, from the drop down select replace then select file I/O palette and from that palette select read from text file as shown in the figure below,
Figure 12: Read block placement
Figure 13: Read to text block
- Again open the help window and see the pins details of this block as well. Also delete the string control you have created in the writing to text file example. This figure below shows the description of the pins of read to text file block in the context help window,
Figure 14: Read to text file pins
- At the output pin named as text, click right and from the dropdown select create and then select indicator, see the figure below,
Figure 15: Indicator placement
- Rename the indicator and on the front panel window resize the string indicator you just created. Now run the VI and the VI will read data from the text file (to which you have written date recently) and print it in the indicator as shown in the figure below,
Figure 16: Output of read to text file
- You can also open a text file, create a text file or replace it using a block from the palette named open/create/replace a file. From the function palette select file I/O and then select open/create/replace file. This block will help you create a new text file in the path you specified in the path control as shown in the figure below,
Figure 17: Open/Create/Replace file
- You can also close a file using a block named close file from the same section in function block as shown in the figure below,
Figure 18: Close a file
- Create and open a file using open/create/replace block, write data in it using write to text file block, read data from it using read to text file block and then close it using close file block in a same VI
(Hint: Use the context help window to see the pin names of the blocks you haven’t used already)