12.5.29    open

Description:   Open a file or command pipeline channel or IO channel

Syntax:         open fileName

open fileName access

open fileName access permissions

         

Argument:     filename access permissions

Returns:        channel identifier of file or port ("handle")

See Also:       file, seek, close, filename, gets, puts

 

Example:      

set fileid [open "../Report1.txt" a+]

seek $fileid 0 start

puts $fileid "----- Water Use Report-------\nPage 1"

#Insert data into table

puts $fileid "[GETVAL %TTMDATE]  [GETVAL %TTMTIME]  [GETVAL AMPLITUDE]  [GETVAL TIMER] "

close $fileid

The open command opens a file, serial port, or command pipeline and returns a channel identifier (a handle) that may be used in future invocations of commands like read, puts, and close.   As a result, open is usually used with a set command to store the "handle" for future use.  A good practice is to use the close command to allow the file to be opened later by other scripts or other instances of your script.

The access argument, if present, indicates the way in which the file (or command pipeline) is to be accessed or created. In the first form access may have any of the following values:

r Open the file for reading only; the file must already exist. This is the default value if access is not specified.

r+ Open the file for both reading and writing; the file must already exist.

w Open the file for writing only. Truncate it if it exists. If it doesn’t exist, create a new file.

w+ Open the file for reading and writing. Truncate it if it exists. If it doesn’t exist, create a new file.

a Open the file for writing only. The file must already exist, and the file is positioned so that new data is appended to the file.

a+ Open the file for reading and writing. If the file doesn’t exist, create a new empty file. Set the initial access position to the end of the file.

HINT

The root for built-in tcl commands used in scripts is the bgr subdirectory for Clients. .

On a Client (View), the root path for tcl is drive:\WebAccess\Client\project_node/bgr

On the Node (ViewDAQ) for tcl and Global scripts, the root path is drive:\WebAccess\Node\project_node/bgr

Note that tcl on VIEW has a root path different that ViewDAQ or Global Scripts on each client or node.

This is also different than the root used to create a file readable using a <WINEXEC> keymacro. If the <WINEXEX> creates a file (for example <WINEXEC> notepad.exe text.txt), the root path is the project_node subdirectory on each client or node.   To create and read files using tcl and WINEXEC try:

<WINEXEC>Notepad.exe ReportClient2.txt

and

 

set fileid [open "../ReportClient2.txt" a+]

puts $fileid "[GETVAL %TTMDATE]  [GETVAL %TTMTIME]  [GETVAL TIMER] "

close $fileid