FileStream Values

A FileStream class implements text file input and output in MAXScript. A FileStream value represents an open text file in MAXScript. Text file I/O is performed by calling functions on the FileStream value.

Constructors

createFile <filename_string>

creates a new file and returns a <filestream> value. If the specified file cannot be created, the value undefined is returned.

openFile <filename_string> [ mode:<mode_string> ]

opens an existing file and returns a <filestream> value. If the specified file does not exist, the value undefined is returned. The optional mode_string can be "a" to mean open an existing file for append output, or "r" to mean open file read-only.

openEncryptedFile <filename> <key>

opens the encrypted file using the given integer key and returns a FileStream value that you can then do read calls on, exactly as you can on FileStreams returned from the openFile() function. See Encrypted Files for details on encrypted files.

Methods

readLine <filestream>

read next line, return as string

readChar <filestream>

read next char, return as string

readChars <filestream> <char_count>

reads the specified number of characters and returns them in a string.

readDelimitedString <filestream> <string>

takes a delimiter character (as a string) and reads in characters until the delimiter is found (or end-of-file is reached) and returns the characters in a string.

skipToString <filestream> <string>

takes a character string and scans forward in the file until it finds an occurrence of the string and positions the file just after that string. If the string is not found, the function returns the value undefined.

skipToNextLine <filestream>

positions the input file at the beginning of the next line

filePos <filestream>

retrieve the current offset into the file

seek <filestream> <integer>

position the file at the given offset so that subsequent I/O will start there.

eof <filestream>

return true if there is no more data in the file, false otherwise.

flush <filestream>

ensure all output to file is on disk, flushes the memory buffers.

close <filestream>

flushes the memory buffers and closes the file

For the following methods, see the description of the execute() method in String Values for information on the scope of variables used in the evaluated expressions.

readValue <filestream>

read and evaluate the next MAXScript <operand> from the file

readExpr <filestream>

read and evaluate the next MAXScript <expr> from the file

execute <filestream>

read and evaluate all the expressions left in the file.

The differences between the above methods can be seen in the following example. In this example, a stringstream value is created that contains a string containing two expressions û random 0. 1. and random red blue. The readValue, readExpr, and execute methods are then used with the stringstream as the argument.

Script

s=stringstream "random 0. 1.;random red blue"

readvalue s      -- read and evaluate the first value

readvalue s      -- read and evaluate the second value

seek s 0         -- position at beginning of stringstream

readexpr s       -- read and evaluate the first expression

readexpr s       -- read and evaluate the second expression

seek s 0         -- position at beginning of stringstream

execute s        -- evaluate all expressions

Output

StringStream:"random 0. 1.;random red blue"  -- result line 1

random()                   -- result line 2 (evaluated "random")

0.0                        -- result line 3 (evaluated "0.")

OK                         -- result line 4

0.448042                   -- result line 5 (evaluated "random 0. 1.")

(color 86.476 0 163.24)    -- result line 6 (evaluated "random red blue")

OK                         -- result line 7

(color 30.2417 0 143.636)  -- result line 7 (evaluated all expressions,

                           --        returns result of last expression)

Associated Methods

print <value> to: <filestream>

format <fmt_string> { <value> } to: <filestream>

See Value Common Properties, Operators, and Methods for a description of these methods.

See also