{ readingWords.p Started by Jeff Ondich on 9/30/96 Last modified on 1/6/97 } program readingWords(input,output,myFile); var theWord, myFileName : string(40); myFile : text; {==================================================== ReadWord reads the next "word" from theFile, which must have been opened before ReadWord is called, returning it via the variable parameter "word". Here, a word consists of a block of contiguous letters and/or apostrophes. Note that ReadWord will read past eoln when it is searching for the next word. ====================================================} procedure ReadWord( var theFile : text; var word : string ); const apostrophe = chr(39); {The apostrophe character has ASCII value 39} type stateType = (beforeWord,duringWord,done); {See pages 198-200 of Abernethy & Allen to see what this is about.} var ch : char; state : stateType; function IsAlpha( c : char ) : boolean; begin IsAlpha := ((c >= 'a') and (c <= 'z')) or ((c >= 'A') and (c <= 'Z')); end; begin word := ''; state := beforeWord; while state <> done do begin if eof(theFile) then state := done else if eoln(theFile) then begin if state = beforeWord then readln(theFile) else state := done end else begin read( theFile, ch ); if (ch = apostrophe) or (IsAlpha(ch)) then begin word := word + ch; state := duringWord end else if state = duringWord then state := done end end end; {end of ReadWord} {========================================================= Main Program =========================================================} begin write( 'File name, please: ' ); readln( myFileName ); reset( myFile, myFileName ); ReadWord( myFile, theWord ); while not eof(myFile) do begin writeln( theWord ); ReadWord( myFile, theWord ) end end.