The following subprograms help deal with end of line and end of file:
As with the Get and Put operations, put a File_Type as the first parameter with these routines if you want to work with a given file, or you'll use the default Current_Input and Current_Output. For example, if you're reading from Startup_File (a variable of type File_Type), you can check for the end of the file by checking "End_Of_File(Startup_File)".
These subprograms are quite useful without being passed any parameters. Note that in Ada, if you call a subprogram but don't want to pass it any parameters, don't include the parentheses() after the name of the subprogram (this is a slightly different syntax than in C and C++).
Here's another demo program, one that only prints ``long'' lines. This demo program illustrates a very common Ada idiom - using ``while (not End_Of_File)'', which processes an entire input file.
with Ada.Strings.Unbounded, Text_IO, Ustrings; use Ada.Strings.Unbounded, Text_IO, Ustrings; procedure Put_Long is -- Print "long" text lines Input : Unbounded_String; begin while (not End_Of_File) loop Get_Line(Input); if Length(Input) > 10 then Put_Line(Input); end if; end loop; end Put_Long;
If you want to discard the rest of a line of input, what subprogram would you use?
|Go back to the previous section||Skip to the next section||Go up to lesson 9 outline|
David A. Wheeler (email@example.com)
The master copy of this file is at
The master copy of this file is at "http://www.adahome.com/Tutorials/Lovelace/s9s2.htm".