You are here

Writing to File

26 July, 2019 - 09:47
Available under Creative Commons-ShareAlike 4.0 International License. Download for free at http://cnx.org/contents/5b6e61df-b830-48cb-9764-94696cb47c80@1.3

Consider the function in Figure Figure 11.1 where a set of numbers in a one-dimensional array represents the resulting noisy signal is to be written to a file. This section will outline the steps required to create files.

media/image1.png
Figure 11.1 Noisy Signal Function  

Create a new G program, right click in the G programming window and select File Dialog from the Functions »Programming »File I/O »Advanced Functions menu. Drag and drop the File Dialog function onto the G programming window.

media/image2.png
Figure 11.2 File Dialog 

The Configure File Dialog dialog box automatically appears to configure the function. Accept the default configuration shown in Figure Figure 11.3 to create a single file by clicking the OK button.

media/image3.png
Figure 11.3 Configure File Dialog  

The resulting diagram after closing the configuration dialog box is shown in . Optionally, right click on File Dialog and select View As Icon from the pop-up menu. This will save some real estate in the G programming window.

media/image4.png
Figure 11.4 G File Dialog 
media/image5.png
Figure 11.5 View As Icon 

From the Functions »Programming »File I/O menu select Open/Create File, Write Binary File and Close File functions.

media/image6.png
Figure 11.6 File Input and Output Operators  

Arrange the File I/O operations as shown in Figure Figure 11.7.

media/image7.png
Figure 11.7 Open, Write and Close File Diagram  

Right click on the operation (0:open) terminal of the Open/Create File function (highlighted in Figure File Create Operation).

media/image8.png
Figure 11.8 File Create Operation  

Select Create » Constant from the pop-up menu.

media/image9.png
Figure 11.9 Create Operation Constant 

Arrange the diagram to look as in Figure Figure 11.10.

media/image10.png
Figure 11.10 Operation Constant 

Click on the down arrow in the operation constant just created and select open or create from the pop-up menu.

media/image11.png
Figure 11.11 Open or Create File Operation 

The resulting updated operation constant value is shown in Figure Figure 11.12.

media/image12.png
Figure 11.12 Create File to Write 

Repeat the process to create a constant for theaccess (0:read/write) terminal (highlighted in Figure Figure 11.13).

media/image13.png
Figure 11.13 File Access Mode 

Set the constant to write-only. Re-arrange the block diagram to look like the diagram shown in Figure Figure 11.14. At this point, the file is set to create a new file for writing.

media/image14.png
Figure 11.14 Write Only Mode 

Get the Noisy Signal function and wire its output data to the Data terminal of the Write to Binary File function.

media/image15.png
Figure 11.15 Writing Binary Data 

Complete the diagram by connecting the Open, Write and Close file operations as shown in Figure Figure 11.16.

media/image16.png
Figure 11.16 Writing to File G Program 

When this G program is executed, the standard file dialog box appears. Name the file to be written signal.dat.

media/image17.png
Figure 11.17 Create File Dialog Box 

Once the program completes executing, the signal.dat file is created and located in the location indicated by the path selected.

media/image18.png
Figure 11.18 Data File signal.dat