-- File: LookAhead.adb -- Add the ASCII values on each line of text. -- Output the results, line by line, and an overall total at the end. -- Demo of: -- Look ahead functionality -- file access -- Character input -- End of line (EOLN) -- Output of enumerated elements -- -- Note: Text file must end with carriage return for this to work with Ada.Text_Io; use Ada; with Ada.Integer_Text_IO; with OpenFile; use OpenFile; procedure LookAhead is type Char_Type is (Alpha, Num, Op, Blank, X); package Class_IO is new Ada.Text_IO.Enumeration_IO(Char_Type); use Class_IO; Class : Char_Type; InFile,OutFile,EnumFile: Text_IO.File_Type; Char: Character; Sumline,Total: Integer; EOL: Boolean; begin OpenReadFile(InFile); -- Get file to process Text_IO.put("Totals Destination:"); Text_IO.New_Line; OpenWriteFile(OutFile); -- Get file for numeric results Text_IO.put("Enum Values Destination:"); Text_IO.New_Line; Openwritefile(Enumfile); -- Get file for numeric results Total:=0; while not Text_Io.End_Of_File (Infile) loop -- Note: Text file must end with CR for this to work SumLine:=0; while not Text_Io.End_Of_Line (Infile) loop Text_Io.Look_Ahead(Infile,Char,Eol); if Eol then Text_Io.Put(" EOL"); else Text_Io.Put("Next:"); Text_Io.Put(Char); End if; Text_IO.Get (File=>InFile, Item=>Char); Text_IO.Put(Char); case Char is when 'A' .. 'Z' | 'a' .. 'z' => Class := Alpha; when '0' .. '9' => Class := Num; when '*' | '+' | '-' | '/' | '<' .. '>' => Class := Op; when ' ' => Class:=Blank; when others => Class := X; end case; SumLine:=SumLine+Character'Pos(Char); Class_IO.Put(Enumfile,Class); Text_Io.Put(Enumfile,' '); -- output to enum file; -- why Text_IO in one only? Is Class_IO necessary? end loop; Integer_Text_IO.put(OutFile,SumLine); Text_IO.New_Line (Outfile); -- Move pointer in OutFile to new line Text_IO.Skip_Line (InFile); -- Move pointer in input buffer past EOL Text_IO.New_Line; Text_IO.New_Line (Enumfile); -- Move pointer in EnumFile to new line Total:=Total+SumLine; end loop; Integer_Text_IO.Put(OutFile,Total); Text_IO.New_Line (Outfile); Text_IO.Close(Infile); Text_IO.Close(Outfile); Text_IO.Close(EnumFile); end LookAhead;