13.1 Advanced Formatted Input and Output

The Mjølner System also 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 defined in the '~beta/basiclib/formatio' library.

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.

In section 8 previously, the example program uses a complex Dump function to print out three numbers and some text. putFormat could have been used instead as illustrated in the following example.

Program 17: StaticAndDynamic2.bet

ORIGIN '~beta/basiclib/formatio'
---- program: descriptor ----
(# 
   (* Static and Dynamic references *)
   
   Point: (# X,Y: @integer;
          enter (X,Y)
          exit  (X,Y)
          #);
   
   refA: ^Point;
   P1,P2: @Point;
   
   Dump:
     (#
     do 'P1: %3d, P2: %d, refA: %d\n'->
           putformat(# do P1.X -> d; P2.X -> d; refA.x -> d #)
     #);

   
do
   'Dynamic references'->putline;
   newline;
   
   &Point[]->refA[];
   
   (1,1)->P1->P2->refA;
   Dump;
   
   (2,2)->P2;
   (3,3)->refA;
   Dump;
   
   P1[]->refA[];  Dump;
   P2[]->refA[];  Dump;
   
   (1,1)->P1; (2,2)->P2; (3,3)->refA;
   Dump;
   
   (111,333)->&Point->P1;
   Dump;
   Newline;
#)

The output is exactly the same as in section 8:

nil% StaticAndDynamic2 
Dynamic references

P1:   1, P2: 1, refA: 1
P1:   1, P2: 2, refA: 3
P1:   1, P2: 2, refA: 1
P1:   1, P2: 2, refA: 2
P1:   1, P2: 3, refA: 3
P1: 111, P2: 3, refA: 3


Libraries Tutorial
© 1994-2004 Mjølner Informatics
[Modified: Thursday January 16th 2003 at 10:23]