Previous Up Next

12.4.3  Writing variable values to a file: write

The write command saves variable values to a file, to be read later.


Example.
Input:

a:=3.14
b:=7
write("foo",a,b)

creates a file named “foo” containing:

a:=(3.14);
b:=7;

If you wanted to store the first million digits of π to a file, you could set it equal to a variable and store it in a file: Input:

pidec:= evalf(pi,10^6):;
write("pi1million",pidec)


The file is written so that it can be loaded with the read command (Section 5.6.2), which simply takes a file name as a string. This allows you to restore the values of variables saved this way, for example in a different session or if you have purged the variables.


Example.
If, in a different session, you want to use the values of a and b above, you can enter:
Input:

read("foo")

This will reassign the values 3.14 and 7 to a and b. Be careful, this will silently overwrite any values that a and b might have had.


Previous Up Next