OPEN
Syntax OPEN filename
(OPEN filename mode)
OPEN []
(OPEN [] mode)
OPEN type-descriptors
(OPEN type-descriptors mode)
 
 
Explanation OPEN prepares for input or output the file specified by its input. When a file has been opened, OPEN returns a channel number which can be used for I/O. The channel number may be assigned to one of the built-in variables STANDARD.INPUT or STANDARD.OUTPUT. Data may then be read using READ, READCHAR, READLINE, READLIST, READQUOTE and other Logo primitives. Data may be written using all Logo commands that print, such as PRINT, or TYPE. If the specified file does not exist, OPEN throws an I/O error.

Many Logo commands that do I/O also accept a channel number as an optional, additional input. The I/O is made using that channel instead of the channel number stored in STANDARD.INPUT or STANDARD.OUTPUT. This makes it easier to perform I/O with multiple channels.

A file is opened for read access in text mode if no other inputs are given. The second input, however, may be a combination of the following characters:
 

r Opens for reading. If the file does not exist or cannot be found, the call fails.
w Opens an empty file for writing. If the given file exists, its contents are destroyed.
a Opens for writing at the end of the file (appending); creates the file first if it does not exist.
r+ Opens for both reading and writing. (The file must exist.)
w+ Opens an empty file for both reading and writing. If the given file exists, its contents are destroyed.
a+ Opens for reading and appending; creates the file first if it does not exist. When a file is opened with the "a" or "a+" access type, all write operations occur at the end of the file. The file pointer can be repositioned, but is always moved back to the end of the file before any write operation is carried out. Thus, existing data cannot be overwritten.
t Open in text (translated) mode. In this mode, CTRL+Z is interpreted as an end-of-file character on input. In files opened for reading/writing with "a+", OPEN checks for a CTRL+Z at the end of the file and removes it, if possible. Also, carriage return–linefeed combinations are translated into single linefeeds on input, and linefeed characters are translated to carriage return–linefeed combinations on output. This is the default I/O mode.
b Open in binary (untranslated) mode; translations involving carriage-return and linefeed characters are suppressed. 

OPEN is also capable of displaying a standard file open/save dialog box. In order to open a dialog box instead of a file, the file name contains a list of file type descriptors. A file type descriptor is a file type, optionally followed by an "=" and a description. The text "lgo=Logo source file" would be a vaild file type descriptor for all files ending with ".LGO". More than one file descriptor may be supplied; in this case, separate the descriptors with commas. The file open/save dialog would display all files ending with the given file type. A Windows dialog would also display the description of the file types, while a Macintosh dialog would display the matching files only. The Logo command OPEN "|lgo=Logo source file,txt=Text file| would, for example, display all files ending with .LGO or .TXT.

If you use "*" as a file type, the file open/save dialog displays all available files. This is done differently on Windows and Macintosh. A Windows dialog would initially display all files with the first type, and offer all additional file types as choices. The Macintosh dialog would always display all files matching all file types.

If you use the empty word or the empty list as a file name, Logo automatically opens a file open/save dialog displaying all available files.

Clicking the Cancel button in a file open/save dialog makes the OPEN command return -1 as the channel number.

See also CLOSE and CREATE.

Examples TO TYPE.FILE :FILE
     MAKE "CHANNEL OPEN :FILE 

     MAKE "STANDARD.INPUT :CHANNEL
     WHILE [NOT EOF?] [PRINT RQ] 
     CLOSE :CHANNEL
END
TYPE.FILE defined
TYPE.FILE "LINES.TXT
This is line 1 
This is line 2 
This is line 3

TopIndex