-- Demonstrates opening, reading, and closing a file whose name is specified within the program
-- We assume that the file contains 0 or more integers.

with Ada.Text_IO; 
with Ada.Integer_Text_IO; 
with ada.command_line;

procedure open_file is

    package cl   renames ada.command_line;
    package tio  renames ada.text_io;
    package itio renames ada.Integer_Text_IO;
    -- Make short names so that we can show where things come from

    my_file: tio.File_Type;             -- Name for file in this program
    os_name: String := "My_Data.txt";   -- OS name for the file
    n: Integer;                         -- Temporary for reading and printing file contents
begin

    -- Open will raise an ADA.IO_EXCEPTIONS.NAME_ERROR expection
    -- if the file does not exist.

    tio.open (file => my_file, mode => tio.In_File, name => os_name);

    loop
        exit when tio.End_Of_File (my_file);

        itio.get (my_file, n);

        itio.put (n);
        tio.new_line;
    end loop;

    tio.close (my_file);

    -- Now do the same thing with each file named as a command line argument
    -- Argument_Count is the number of command line arguments

    tio.put_line("Argument_count: " & cl.Argument_count'img);
    for I in 1 .. cl.argument_count loop

        -- Output each command line argument
        tio.put (cl.argument(i));
        tio.put_line (":");

        -- Open each file named by the command line argument

        tio.open (my_file, tio.In_File, cl.argument(i));

        loop
            exit when tio.End_Of_File (my_file);

            itio.get (my_file, n);

            itio.put (n);
            tio.new_line;
        end loop;

        tio.close (File => my_file);

    end loop;

end open_file;