Technical data
Compiler Directives
2-27
Output File Control
The structure of the output file control construct is:
%openfile string optional-equal-string optional-mode
%closefile id
%selectfile id
The %openfile directive opens a file or buffer for writing; the required string
variable becomes a variable of type
file. For example:
%openfile x /% Opens and selects x for writing. %/
%openfile out = "out.h" /% Opens "out.h" for writing. %/
The %selectfile directive selects the file specified by the variable as the
current output stream. All output goes to that file until another file is selected
using
%selectfile. For example:
%selectfile x /% Select file x for output. %/
The %closefile directive closes the specified file or buffer, and if this file is the
currently selected stream,
%closefile invokes %selectfile to reselect the last
previously selected output stream.
There are two possible cases that
%closefile must handle:
•If the stream is a file, the associated variable is removed as if by
%undef.
• If the stream is a buffer, the associated variable receives all the text that has
been output to the stream. For example:
%assign x = "" /% Creates an empty string. %/
%openfile x
"hello, world"
%closefile x /% x = "hello, world\n"%/
If desired, you can append to an output file or string by using the optional
mode,
a, as in:
%openfile "foo.c", "a" %% Opens foo.c for appending.