×
Menu
Index

OPEN, CLOSE Subroutines

 
Usage:
n = OPEN(name [,"in" | "in-out" | "in-out-trunc" | "uin" | "uin-out" | "uin-out-trunc" [, "binary"]])
CLOSE(n)
 
Description:
The OPEN and CLOSE subroutines are used to open a file for access and then close it. The optional open methods specify how the file should be opened. The "in" option opens an existing file as read-only; "in-out" (default) opens or creates a file that can be read or written; "in-out-trunc" will truncate an existing file to zero or create a new file that can be read or written. The "uin" variety of open methods in non-binary mode will look for a Unicode BOM at the beginning of the file and automatically translate the data as needed. The optional “binary” parameter is available in TBSVER 2 or later and treats the data to read/write as binary data (not text strings).
 
Opened files that are not read-only can be written to using WRITEL, and all files can be read from using READL. The current version only supports reading and writing lines of text.
 
This subroutine returns -1 if there was a problem opening the file and sets member .errno containing a failure code.
 
NOTE: Although the script interpreter will make sure that all opened files are closed eventually, you should explicitly close any files you open. This will prevent you from running out of file handles if your script needs to open several files.
 
Example:
sub main()
   // Open a file
   f = open("file1.txt", "uin-out")
 
   // Move to the end of any existing text
   seek(f, lof(f))
 
   // Write 50 lines of text
   for i = 1 to 50
      s = "This is test line " # i # "!!!"
      writel(f, s)
   next
 
   // Write one blank line
   writel(f)
 
   // Close file
   close(f)
end sub