4 The formatio Library

4.1 Formatted input/output

This library provides facilities for formatted input and output (similar to the scanf and printf functions in C). These facilities are implemented in the form of the getFormat and putFormat operations that are added as stream attributes, making formatted input and output available for any stream.

4.2 Format string

Both getFormat and putFormat take a text string as argument. This text string must contain a format specification of the input to be read from (respectively output to) the stream. The format string may be any string, possibly with one or more embedded markers. The markers specify the variable parts of the expected input (respectively output), such as integer values. The markers are indicated in the string by a leading '%'. Following the '%' is the specification of the marker type.

getFormat accepts the following marker syntax:


putFormat accepts the following marker syntax:

%-+ [[0]width][.[[0]precision]]dioxXrRbBfeEgGcsn%

As it can be seen, the marker syntax is very similar.

4.3 Marker type

Corresponding to every marker type (given by the dioxXrRbBfeEgGcsn part of the marker syntax), getFormat and putFormat defines an attribute (with the same name) as the marker symbol an attribute with the name d, corresponding to the d marker type. Due to BETA not being case-sensitive in identifiers, the attributes corresponding to the upper-case marker types are called e.g. uG for upper g.

The functionality of formatted input and output can now easiest be described by showing the following example:

(# t, s: @text;
   theName: ^text; theValue: integer;
do 'name: temperature value: 72 name: speed value: 35' -> t;
   'name: %s value: %d'
   t.getFormat(# do s->theName[];  d->theValue #);
   'The name is %s and the value is %d\n'
   s.putFormat(# do theName[]->s; theValue->d #);
   'name: %s value: %d'
   t.getFormat(# do s->theName[];  d->theValue #);
   'The name is: %s and the value is: %d\n'
   s.putFormat(# do theName[]->s; theValue->d #)

At the end of this program, s will contain the following text:

The name is: temperature and the value is: 72
The name is: speed and the value is: 35

The getFormat and putFormat operations raise exceptions if the format specifications are not satisfied.

Basic Libraries - Reference Manual
© 1990-2002 Mjølner Informatics
[Modified: Friday October 27th 2000 at 13:15]