Simple File IO


-- Simple File IO

with Ada.Text_IO; use Ada.Text_IO;

procedure Simple_FileIO is
   My_File   : File_Type;
   File_Name : String := "test.txt";
begin
   Put_Line("Create a file: " & File_Name);

   Create(File => My_File,
          Mode => Out_File,
          Name => File_Name);

   Put_Line(File => My_File,
            Item => "Hello World!");


   Put_Line("Open file: " & File_Name);

   Reset (File => My_File,
          Mode => In_File);

--     The following would also work instead of Reset:
--
--     Close(File => My_File);
--
--     Open(File => My_File,
--          Mode => In_File,
--          Name => File_Name);

   declare
      Line_Length       : Natural;
      Max_String_Length : constant := 255;
      Output_String     : String (1 .. Max_String_Length);
   begin
      Get_Line(File => My_File,
               Item => Output_String,
               Last => Line_Length);

      Put_Line(Output_String (1 .. Line_Length));
   end;

   Delete(File => My_File);

--    The file could also just be closed instead of deleted
--    Close(File => My_File);


end Simple_FileIO;


Contributed by: David Botton
Contributed on: May 18, 1999
License: Public Domain

Back