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 returns True if the input is at the end of the line (else it returns False).
Function End_Of_File
End_Of_File returns True if the input is at the end of the file (else it returns False).
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 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;


Quiz:

If you want to discard the rest of a line of input, what subprogram would you use?

  1. New_Line
  2. Skip_Line
  3. End_Of_Line

You may also:

PREVIOUS Go back to the previous section

NEXT     Skip to the next section

OUTLINE  Go up to lesson 9 outline

David A. Wheeler (dwheeler@dwheeler.com)

The master copy of this file is at "http://www.adahome.com/Tutorials/Lovelace/s9s2.htm".