AdaPower Logged in as Guest
Ada Tools and Resources

Ada 95 Reference Manual
Ada Source Code Treasury
Bindings and Packages
Ada FAQ


Join >
Home >
Articles >
Getting Started >
Ada FAQ >
Source Treasury >
Books & Tutorials >
Packages for Reuse >
Latest Additions >
Ada Projects >
Press Releases >
Ada Audio / Video >
Home Pages >
Links >
Contact >
About >
Login >
Back
Simple File-IO (David Botton)

-- 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;


(c) 1998-2004 All Rights Reserved David Botton