184.108.40.206 File I/O Directives
You may open, read, write, append, and close plain ASCII text files while parsing POV-Ray scenes. This feature is
primarily intended to help pass information between frames of an animation. Values such as an object's position can be
written while parsing the current frame and read back during the next frame. Clever use of this feature could allow a
POV-Ray scene to generate its own include files or write self-modifying scripts. We trust that users will come up with
other interesting uses for this feature.
Note: some platform versions of POV-Ray (e.g. Windows) provide means to restrict the
ability of scene files to read & write files.
220.127.116.11.1 The #fopen Directive
Users may open a text file using the
#fopen directive. The syntax is as follows:
#fopen IDENTIFIER "filename" OPEN_TYPE
read | write | append
Where IDENTIFIER is an undefined identifier used to reference this file as a file handle, "filename"
is any string literal or string expression which specifies the file name. Files opened with the
open for read only. Those opened with
write create a new file with the specified name and it overwrites
any existing file with that name. Those opened with
append opens a file for writing but appends the text
to the end of any existing file.
The file handle identifier created by
#fopen is always global and remains in effect (and the file
remains open) until the scene parsing is complete or until you
#fclose the file. You may use
FILE_HANDLE_IDENTIFIER to see if a file is open.
18.104.22.168.2 The #fclose Directive
Files opened with the
#fopen directive are automatically closed when scene parsing completes however
you may close a file using the
#fclose directive. The syntax is as follows:
Where FILE_HANDLE_IDENTIFIER is previously opened file opened with the
#fopen directive. See
"The #fopen Directive".
22.214.171.124.3 The #read Directive
You may read string, float or vector values from a plain ASCII text file directly into POV-Ray variables using the
#read directive. The file must first be opened in "read" mode using the #fopen directive. The syntax for
#read is as follows:
#read (FILE_HANDLE_IDENTIFIER, DATA_IDENTIFIER[,DATA_IDENTIFIER]..)
UNDECLARED_IDENTIFIER | FLOAT_IDENTIFIER | VECTOR_IDENTIFIER |
Where FILE_HANDLE_IDENTIFIER is the previously opened file. It is followed by one or more DATA_IDENTIFIERs
separated by commas. The parentheses around the identifier list are required. A DATA_IDENTIFIER is any
undeclared identifier or any previously declared string identifier, float identifier, or vector identifier. Undefined
identifiers will be turned into global identifiers of the type determined by the data which is read. Previously
defined identifiers remain at whatever global/local status they had when originally created. Type checking is
performed to insure that the proper type data is read into these identifiers.
The format of the data to be read must be a series of valid string literals, float literals, or vector literals
separated by commas. Expressions or identifiers are not permitted in the data file however unary minus signs and
exponential notation are permitted on float values.
If you attempt to read past end-of-file, the file is automatically closed and the FILE_HANDLE_IDENTIFIER
is deleted from the symbol table. This means that the boolean function
can be used to detect end-of-file. For example:
#fopen MyFile "mydata.txt" read
126.96.36.199.4 The #write Directive
You may write string, float or vector values to a plain ASCII text file from POV-Ray variables using the
directive. The file must first be opened in either
append mode using the
directive. The syntax for
#write is as follows:
#write( FILE_HANDLE_IDENTIFIER, DATA_ITEM[,DATA_ITEM]...)
FLOAT | VECTOR | STRING
Where FILE_HANDLE_IDENTIFIER is the previously opened file. It is followed by one or more DATA_ITEMs
separated by commas. The parentheses around the identifier list are required. A DATA_ITEM is any valid string
expression, float expression, or vector expression. Float expressions are evaluated and written as signed float
literals. If you require format control, you should use the
str(VALUE,L,P) function to convert it to a
formatted string. See "String Functions" for details on the
function. Vector expressions are evaluated into three signed float constants and are written with angle brackets and
commas in standard POV-Ray vector notation. String expressions are evaluated and written as specified.
Note: data read by the
#read directive must have comma delimiters
between values and quotes around string data but the
#write directive does not automatically output
commas or quotes.
For example the following
#read directive reads a string, float and vector.
It expects to read something like:
"A quote delimited string", -123.45, <1,2,-3>
The POV-Ray code to write this might be:
#declare Val1 = -123.45;
#declare Vect1 = <1,2,-3>;
#write(MyFile,"\"A quote delimited string\",",Val1,",",Vect1,"\n")
See "String Literals" and "Text
Formatting" for details on writing special characters such as quotes, newline, etc.