Go to the first, previous, next, last section, table of contents.

I/O Examples

Here are some short programs that demonstrate using simple I/O. The first example uses only standard input and standard output, so reader and writer variables do not explicitly appear.

MODULE Main;
IMPORT IO;

VAR x: INTEGER;

BEGIN
  IO.Put("Welcome.  Enter a number: ");
  x := IO.GetInt();

  IF x < 0 THEN
    IO.Put("The number was negative");
  ELSE
    IO.Put("The number was not negative.");
  ENDIF
END Main.

The next example does some simple file I/O. It introduces reader and writer variables:

MODULE Main;
IMPORT IO, Rd, Wr;

VAR inFile: Rd.T;
    outFile: Wr.T;
    num: INTEGER;

BEGIN
  outFile := IO.OpenWrite("numbers.dat");
  FOR i := 1 TO 10 DO 
    IO.PutInt(i, outFile);
    IO.Put("\n", outFile); 
  END;
  Wr.Close(outFile);

  inFile := IO.OpenRead("numbers.dat");
  REPEAT
    num := IO.GetInt(inFile);
  UNTIL num = 10;
  Rd.Close(inFile);

END Main.


Go to the first, previous, next, last section, table of contents.