home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / m / mod2tutr.zip / EXAMPLES.ZIP / CH08E2.MOD < prev    next >
Text File  |  1989-01-18  |  3KB  |  83 lines

  1.                             (* Chapter 8 - Programming exercise 2 *)
  2. MODULE CH08E2;
  3.  
  4. FROM FileSystem IMPORT Lookup, Close, File, Response, ReadChar;
  5. FROM InOut      IMPORT Write, WriteString, WriteCard, ReadString,
  6.                                                        WriteLn, EOL;
  7.  
  8. VAR  NameOfFile : ARRAY[1..15] OF CHAR;
  9.      InFile     : File;
  10.      InLine     : ARRAY[1..80] OF CHAR;  (* Note, There is no check
  11.                                               for a spill out of
  12.                                               this line.  Assumes all
  13.                                               lines are shorter.   *)
  14.      Character  : CHAR;
  15.      Count      : CARDINAL;
  16.      ENDCount   : CARDINAL;
  17.  
  18.    PROCEDURE CheckForEND;
  19.    VAR ENDFound : BOOLEAN;
  20.        Index    : INTEGER;
  21.    BEGIN
  22.       ENDFound := FALSE;
  23.       FOR Index := 1 TO (INTEGER(Count) - 2) DO
  24.          IF (InLine[Index] = 'E') AND
  25.                        (InLine[Index + 1] = 'N') AND
  26.                                   (InLine[Index + 2] = 'D') THEN
  27.             ENDFound := TRUE;
  28.             ENDCount := ENDCount + 1;
  29.          END;
  30.       END;
  31.  
  32.       IF ENDFound THEN
  33.          WriteString(InLine);
  34.          WriteLn;
  35.       END;
  36.  
  37.    END CheckForEND;
  38.  
  39. BEGIN
  40.    REPEAT                  (* repeat until a good filename is found *)
  41.       WriteLn;
  42.       WriteString("Enter name of file to display ---> ");
  43.       ReadString(NameOfFile);
  44.       Lookup(InFile,NameOfFile,FALSE);
  45.    UNTIL InFile.res = done;                  (* good filename found *)
  46.    ENDCount := 0;
  47.    WriteLn;
  48.    Count := 0;
  49.    InLine[1] := 0C;                            (* String terminator *)
  50.    REPEAT       (* character read/display loop - quit at InFile.eof *)
  51.       ReadChar(InFile,Character);
  52.       IF NOT InFile.eof THEN
  53.          IF Character # EOL THEN
  54.             Count := Count + 1;
  55.             InLine[Count] := Character;
  56.             InLine[Count + 1] := 0C;           (* String terminator *)
  57.          ELSE
  58.             CheckForEND;
  59.             Count := 0;
  60.             InLine[1] := 0C;                   (* String terminator *)
  61.          END;
  62.       END;
  63.    UNTIL InFile.eof;                      (* quit when eof is found *)
  64.    Close(InFile);
  65.  
  66.    WriteLn;
  67.    WriteString("There were");
  68.    WriteCard(ENDCount,5);
  69.    WriteString(" END's in the file.");
  70.    WriteLn;
  71. END CH08E2.
  72.  
  73.  
  74.  
  75.  
  76. (* Result of execution
  77.  
  78. (The selected file is listed on the monitor, with the character
  79.   count listed for each line, and the total number of lines.)
  80.  
  81. *)
  82.  
  83.