next_inactive up previous contents
Up: The MPD Programming Language Previous: Resources and Globals   Contents

Subsections

Basic Input and Output

Files and File Access

File input and output is supported by an additional data type, file, and several operation on this type. A variable of type file contains a file descriptor pointing to a file. There are five predefined file literals, all of which are reserved words.

stdin the standard input device (usually the keyboard)
stdout the standard output device (usually the tty)
stderr the standard error device (usually the tty)

The other two literals are null and noop. Attempting to access a through a file descriptor which value is null results in an error. Attempting to read from a file descriptor which value is noop returns an immediate end of file indication.; other operations has no effect. In addition to this, there are two predefined enumeration types

type accessmode = enum(READ, WRITE, READWRITE)
type seektype   = enum(ABSOLUTE, RELATIVE, EXTEND)

These types define the enumeration literals used as arguments to the open and seek operations. Finally, there is one predefined integer constant:

const EOF = -1

As mentioned above, EOF is sometimes the return value of file access operations. The following functions are defined on files

open opens a file for reading
close closes an open file
flush flushes any data currently in the output buffer
remove removes the file

Consider the following example which tries to open the file foo and prints an error message if it fails.

file f
f = open("foo", READ) # open the file foo for reading
if (f == null) { 
  write("Error: cannot open file"
  stop(1)
}

Simple I/O

The functions read, write and writes provides you with the simplest type of unformatted I/O. The first argument to the functions specifies the file to which the function applies; this argument is optional and if omitted stdin is used for the read function while stdout is used for the write functions. For example, the two read invocations are equivalent, as are the two write invocations.

read(stdin, x, y)
read(x, y)
write(stdout, x)
write(x)

The general forms of read and write are

read ( variable, variable, ...)
write ( expr, expr, ... )

Formatted I/O

The functions printf and scanf give you more control on the format of the output and input resp. The are similar to their namesakes in the C language. The variants sprintf and sscanf are similar except that the output is written to a string instead of a file.

The general form of an invocation of printf is

printf(f, fmt, x1, x2, ...)

The f indicates which file the result should be written to and can be omitted in the same manner as above. The fmt argument is the format string which specifies how the following parameters should be interpreted and,thus , output. See appendix C for details on the format string.

The scanf function is similar to the printf function. Its general form is.

scanf(f, fmt, x1, x2, ...)

The purpose of the scanf function is opposite the function of the printf function in that that it reads formatted data from a file rather than writing formatted data to a file. The format string is identical. scanf returns the number of successful conversion.

The sprintf and sscanf are identical to printf and scanf with the exception that the first parameter should be a string instead of a file and that the parameter cannot be omitted.

Consider the following example

printf("a[%d] is %d\n", i, a[i]) 
# prints the given format string with the %d 
# replaced by the value of i and a[i] interpreted as integers

if (sscanf(s, "%2d%3d",a,b) != 2) {
  write(stderr,"bad format")
  stop(1)
}       # interpret the string as a 2 literal number followed 
        # by a 3 literal number.
        # exit with an error if the conversion fails

Character I/O

The functions get and put treat the input and output as being stream of characters without interpretation. The get function takes as its first argument the file to read from and as its second argument the string to store the read input to. It returns the number of character actually read. If the file contains maxlength(str) character, that many are read. If the end of the file is reached EOF is returned. get has the following specification

get(file f, string res) returns int

The put function outputs the contents of its second argument into the file specified by its first argument or to stdout if the first argument is omitted. put has the following specification

put(file f, string str)

Random Access

The seek and where functions are used for random access in a file. A read/write pointer is associated with ever file. It records the offset within the file where the next I/O operation will apply. Invoking seek moves the pointer and returns the new pointer value, while where returns its current position. The specification of seek s

seek(file f, seektype t, int offset)

The effect of seek depends on the seek type, specified by t, as follows:
ABSOLUTE set pointer to offset
RELATIVE add offset to current pointer
EXTEND set to the end of file plus offset
The where function has the following specification:

where(file f)) returns int pos

It returns the current position of the read/write pointer in file f.

The following example illustrates the seek function:

file f
int e
f = open("foo", READWRITE)
e = seek(f, EXTEND, 0) # find length of file
for [i = 0 to e by 2] {
  seek(f, ABSOLUTE, 1)
  put(f,"*")
}
close f

Command Line Arguments

Command line arguments are accessed via the two functions numargs and getarg. The numargs function returns the number of user supplied arguments. The getarg function reads the command line argument specified positionally by its first argument into its second argument. The type of its second argument can be int, bool, char, real, string, array of char, pointer or enum. The getarg function returns a positive number if successful and a nonpositive number otherwise; see appendix C for detail.

External Operations

MPD supports external operations, i.e. function calls to functions written in other languages. This is beyond the scope of this summary.


next_inactive up previous contents
Up: The MPD Programming Language Previous: Resources and Globals   Contents
David Sands 2003-09-05