Asymptote
can read and write text files (including comma-separated
value) files and portable XDR (External Data Representation)
binary files.
An input file must first be opened with
input(string name, bool check=true, string commentchar="#")
;
reading is then done by assignment:
file fin=input("test.txt"); real a=fin;
If the optional boolean argument check
is false
, no check will
be made that the file exists. If the file does not exist or is not
readable, the function bool error(file)
will return true
.
The first character of the string commentchar
specifies a
comment character. If this character is encountered in a data file,
the remainder of the line is ignored. When reading strings, the
comment character must be in the first column (otherwise it will be
treated as an ordinary character).
One can change the current working directory with the
string cd(string)
function, which returns the new working directory.
When reading pairs, the enclosing parenthesis are optional.
Strings are also read by assignment, by reading characters up to but not
including a newline. In addition, Asymptote
provides the function
string getc(file)
to read the next character only, returning it
as a string.
A file named name
can be open for output with
file output(string name, bool append=false);data will be appended to an existing file only if the file is opened with
append=true
.
Data of a built-in type T
can be written to an output file by
calling one of the functions
write(string s="", T x, suffix e=endl ... T[]); write(file fout, string s="", T x, suffix e=none ... T[]); write(file fout=stdout, string s="", explicit T[] x ... T[][]); write(file fout=stdout, T[][]); write(file fout=stdout, T[][][]); write(file fout=stdout, suffix e=endl);If the
fout
is not specified, stdout
is used and
terminated with a newline. If specified, the optional
identifying string s
is written before the data x
.
An arbitrary number of data values may be listed when writing scalars
or one-dimensional arrays. The suffix e
may be one of the following:
none
(do nothing), endl
(terminate with a newline), or
tab
(terminate with a tab). Here is a simple example of data output:
file fout=output("test.txt"); write(fout,1); // Writes "1" write(fout); // Writes a new line write(fout,"List: ",1,2,3); // Writes "List: 1 2 3"There are two special files:
stdin
, which reads from the keyboard,
and stdout
, which writes to the terminal.
A file may also be opened with xinput
or xoutput
instead of
input
or output
, in which case it will read or write
double precision values written in Sun Microsystem's XDR (External
Data Representation) portable binary format (available on all UNIX platforms).
The function file single(file)
sets the file to read single
precision XDR values; calling file single(file,false)
sets it
back to read doubles again. The default initializer for file is stdout
.
One can test a file for end-of-file with the boolean function eof(file)
,
end-of-line with eol(file)
, and for I/O errors with error(file)
.
One can flush the output buffers with flush(file)
, clear a
previous I/O error with clear(file)
, and close the file with
close(file)
. To set the number of digits of output precision, use
precision(file,int)
. The function int tell(file)
returns
the current position in an input file relative to the beginning.
The function seek(file, int)
can be used to change this
position; for example, to rewind a file, use the command seek(file,0)
.
string getstring(string name="", string default="", string prompt="", bool save=true); int getint(string name="", int default=0, string prompt="", bool save=true); real getreal(string name="", real default=0, string prompt="", bool save=true); pair getpair(string name="", pair default=0, string prompt="", bool save=true)defined in the module
plain
may be used to prompt for a value from
stdin
using the GNU readline
library.
If save=true
, the history of values for name
is
saved to the file ".asy_"+name
(see history). The most
recent value in the history will be used to provide a default value
for subsequent runs. The default value (initially default
) is
displayed after prompt
. These routines are based on the
following interface to readline
, which prompts the user with
the default value formatted according to prompt
and
saves the local history under the name ".asy_"+history
, unless
the string history
begins with a linefeed ('\n'
):
string readline(string prompt="", string history="", string initial="", bool tabcompletion=false);