Text files

Access to text files is available through the use of the TextOpen(), TextClose(), TextRead(), and TextWrite() Intrinsic Functions.

Access to a textfile is initiated using TextOpen(). This function takes the filename, and an access specifier, and returns a file handle. The file handle is an integer which is associated by EASI with the open file, and is used in all subsequent accessto that file, until it is closed. The access specifiers may be either "r" for read-only access to the file, "w" for write access to the file, or "a" for append access to the file.

For example, to open an already existing text file called "data.txt":

local integer fd
fd = TextOpen( "data.txt", "r" )

Once the file has been opened, the TextRead() intrinsic can be used to read a line of data.

local string dataline
dataline = TextRead( fd )

The TextRead() intrinsic is given the handle (in this case, fd) of the open which is to be read. The first time it is called, it will return the first line of the text file. On each subsequent call, it will return the next line of the text file. When the end of the file is reached, it will return the string "<EOF>".

Once access to a file is no longer needed, it should be closed using TextClose().

call TextClose( fd )

In order to create a new file, it should be opened with "w" access. Should the file already exist, its contents will be deleted. Once open, the new file may be written to using the TextWrite() intrinsic:

fd = TextOpen( "new.txt", "w" )
call TextWrite( fd, "a line of text to be written to the file" )
call TextClose( fd )

TextWrite() may be called as many times as desired to write more lines to the file. Lines can be appended to the end of an existing file by opening it with "a" access. If the file does not already exist, then a new one will be created.

fd = TextOpen( "file.txt", "a" )
call TextWrite( fd, "a line of data to be appended to the file" )
call TextClose( fd ).

TextImport() is a convenient intrinsic that allows an entire text file to be imported directly into an MSTRING. It requires only the file name as an argument, and there is no need to open and close the file to use TextImport().

local mstring textdata
textdata = TextImport( "filename.txt" )

Similarly, TextExport() allows an mstring to be written directly to a text file.

call TextExport( "newfile.txt", textdata )

© PCI Geomatics Enterprises, Inc.®, 2026. All rights reserved.