You are here

Writing to File

26 July, 2019 - 09:47
Available under Creative Commons-ShareAlike 4.0 International License. Download for free at

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.

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.

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.

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.

Figure 11.4 G File Dialog 
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.

Figure 11.6 File Input and Output Operators  

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

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

Figure 11.8 File Create Operation  

Select Create » Constant from the pop-up menu.

Figure 11.9 Create Operation Constant 

Arrange the diagram to look as in Figure Figure 11.10.

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.

Figure 11.11 Open or Create File Operation 

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

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

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.

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.

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.

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.

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.

Figure 11.18 Data File signal.dat