Section 9.2 - Line and File Endings
Package Text_IO models text files as a sequence of text lines;
each text line has zero or more characters in it.
Different operating systems have different ways to indicate
the end of a line and the end of a file,
so Text_IO detects them using the local operating system's conventions.
The following subprograms help deal with
end of line and end of file:
- Procedure New_Line
-
New_Line ends the current line and starts a new line.
It takes an optional parameter indicating how many new lines to create
(the default is one).
You can also specify the file to output this new line to
(the default is Current_Output).
- Procedure Skip_Line
-
Skip_Line is the counterpart of New_Line;
it gets ready to read the line after the current line,
discarding any text on the current line that hasn't been read.
- Function End_Of_Line
-
End_Of_Line detects whether or not the input is at the end of the line.
- Function End_Of_File
-
End_Of_File detects whether or not the input is at the end of the file.
- Function Line
-
Line reports the current line number of the file you're reading or writing
(the first line is number 1).
This is useful if you're processing some input data and you've suddenly
found an input problem.
As with the Get and Put operations, put a File_Type as the first parameter
if you want to work with a given file, or you'll use the default
Current_Input and Current_Output.
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 slightly different than 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;
Quiz:
If you want to discard the rest of
a line of input, what subprogram would you use?
- New_Line
- Skip_Line
- End_Of_Line
You may also:
Go back to the previous section
Skip to the next section
Go up to the outline of lesson 9
David A. Wheeler (wheeler@ida.org)