## `write` -- write the values of variables into a file

#### Introduction

`write``(filename)` stores all assigned identifiers of the MuPAD session with their current values in a file specified by `filename`.

`write``(filename, x1, x2...)` stores the current values of the identifiers `x1`, `x2` etc.

`write(`n`)` and `write(`n, x1, x2...`)` store the data in the file associated with the file descriptor `n`.

#### Call(s)

`write( <format>, filename)`
`write( <format>, filename, x1, x2...)`
`write(n)`
`write(n, x1, x2...)`

#### Parameters

`filename` | - | the name of a file: a character string
`x1, x2...` | - | identifiers
`n` | - | a file descriptor provided by `fopen`: a nonnegative integer

#### Options

`format` | - | the write format: either Bin or Text. With Bin, the data are stored in MuPAD's binary format. With Text, standard ASCII format is used. The default is Bin.

#### Returns

the void object of type `DOM_NULL`.

#### Side Effects

The function is sensitive to the environment variable `WRITEPATH`. If this variable has a value, the file is created in the corresponding directory. Otherwise, the file is created in the ``working directory''.

#### Related Functions

`fclose`, `finput`, `fopen`, `fprint`, `fread`, `ftextinput`, `pathname`, `print`, `protocol`, `read`, `READPATH`, `WRITEPATH`

#### Details

- `write` serves for storing information from the current MuPAD session in a file. The file contains the values of identifiers of the current session. These identifiers are assigned the stored values when this file is read into another MuPAD session via the function `read`.
- The file may be specified directly by its name. In this case, `write` creates a new file or overwrites an existing file; `write` opens and closes the file automatically.

  If `WRITEPATH` does not have a value, `write` interprets the file name as a pathname relative to the ``working directory''.

  Note that the meaning of ``working directory'' depends on the operating system. On Windows systems, the ``working directory'' is the folder where MuPAD is installed. On UNIX or Linux systems, it is the current working directory in which MuPAD was started.

  On the Macintosh, an empty file name may be given. In this case, a dialogue box is opened in which the user can choose a file. Further, on the interactive level, MacMuPAD warns the user, if an existing file is about to be overwritten.

  Also absolute path names are processed by `write`.

- Instead of a file name, also a file descriptor of a file opened via `fopen` can be used. Cf. example 2. In this case, the data written by `write` are appended to the corresponding file. The file is not closed automatically by `write` and must be closed by a subsequent call to `fclose`.

  Note that `fopen``(filename)` opens the file in read-only mode. A subsequent `write` command to this file causes an error. Use the Write or Append option of `fopen` to open the file for writing.

  The file descriptor `0` represents the screen.

- `write` stores the values of the given identifiers, not their full evaluation! Cf. example 3.

- `write` is a function of the system kernel.

#### Option: Text

- In ASCII format, assignments of the form

  `sysassign(identifier, hold(value)):`

  are written into the file. Cf. example 1.

#### Example 1

The variable `a` and its value `b + 1` are stored in a file named `test`:

    >> a := b + 1: write(Text, "test", a):

The content of this file is displayed via `ftextinput`:

    >> ftextinput("test")

                           "sysassign(a, hold(b + 1)):"

We delete the value of `a`. Reading the file `test` restores the previous value:

    >> delete a: read("test"): a

                                       b + 1

    >> delete a:

#### Example 2

The file `test` is opened for writing using MuPAD's binary format:

    >> n := fopen("test", Write)

                                        18

This number is the descriptor of the file and can be used in a write command:

    >> a := b + 1: write(n, a):

    >> delete a: read("test"): a

                                       b + 1

We close the file and clean up:

    >> fclose(n): delete n, a:

#### Example 3

The value `b + 1` is assigned to the identifier `a`. After assigning the value `2` to `b`, complete evaluation of `a` yields `3`:

    >> a := b + 1: b := 2: a

                                         3

Note, however, that the value of `a` is the expression `b + 1`. This value is stored by a `write` command:

    >> write(Text, "test", a): ftextinput("test")

                           "sysassign(a, hold(b + 1)):"

Consequently, this value is restored after reading the file into a MuPAD session:

    >> delete a, b: read("test"): a

                                       b + 1

    >> delete a:

#### Changes

- no changes

Do you have questions or comments?
Copyright © SciFace Software GmbH & Co. KG 2000