dos_compilers/Logitech Modula-2 v1.1/EXAMP2.MOD
2024-06-30 15:43:04 -07:00

47 lines
1.6 KiB
Modula-2
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

MODULE Examp2; (* Program Module to demonstrate basic file I/O *)
IMPORT InOut;
FROM InOut IMPORT
OpenInput, OpenOutput, CloseInput, CloseOutput, Read, Write, EOL;
IMPORT Terminal; (* get qualified access to Terminal routines *)
IMPORT CardinalIO;
CONST ESC = 33C;
VAR Ch: CHAR;
LinesToCopy, LinesCopied: CARDINAL;
BEGIN
(* Note that interaction with user is done via Terminal and CardinalIO,
* because InOut input/output is being redirected to files.
*)
REPEAT
Terminal.WriteString("enter input file:"); Terminal.WriteLn;
OpenInput(""); (* request input file, no default extension *)
UNTIL InOut.Done; (* keep trying until open is successful *)
REPEAT
Terminal.WriteString("Lines to copy> ");
CardinalIO.ReadCardinal(LinesToCopy); Terminal.WriteLn;
Terminal.Read(Ch); (* read terminator of ReadCardinal *)
UNTIL Ch <> ESC; (* keep asking until entry ends with ' ' or EOL *)
REPEAT
Terminal.WriteString("enter output file:"); Terminal.WriteLn;
OpenOutput(""); (* request output file, no default extension *)
UNTIL InOut.Done;
LinesCopied := 0;
LOOP
IF LinesCopied >= LinesToCopy THEN EXIT END;
Read(Ch); (* read from in file *)
IF NOT InOut.Done THEN EXIT END; (* check for EOF *)
Write(Ch); (* otherwise copy char *)
IF Ch = EOL THEN INC(LinesCopied) END;
END; (* LOOP *)
IF LinesCopied < LinesToCopy THEN
Terminal.WriteString("[Only ");
CardinalIO.WriteCardinal(LinesCopied,0);
Terminal.WriteString(" lines in file]"); Terminal.WriteLn;
END;
CloseOutput;
CloseInput;
END Examp2.