[gtkada] working with texts files

Emmanuel Briot briot at ACT-Europe.FR
Tue Nov 27 12:46:36 CET 2001


"luix" <luix at tropicalstorm.com> writes:

> I know it's ages since I'm boring you with this thing, but I haven't be
> able to do it work properly.
> I would be really pleased if anyone could send a complete program where you
> write and read from a text file to a gtk_text widget, at least include the
> declaration and the two procedures.


Here is some Ada code that might help. Definitely not a full program, but the
core is there (reading the file and inserting in the widget. You'll still have
to take care of freeing the memory, creating the text widget, testing that the
file was correctly open,...)
This is highly GNAT specific, of course :-)


Emmanuel




   --  Name is the name of the file
   --  Widget is your Gtk_Text widget

   with Ada.Text_IO;             use Ada.Text_IO;
   with GNAT.OS_Lib;             use GNAT.OS_Lib;

   declare
      F           : File_Descriptor;
      Length      : Long_Integer;
      Name_Zero   : aliased constant String := Name & ASCII.NUL;
      Buffer      : String_Access;
   begin
      F := Open_Read (Name_Zero'Address, Text);

      if F = Invalid_FD then
         raise Name_Error;
      end if;

      Length := File_Length (F);
      Buffer := new String (1 .. Positive (Length));
      Length := Long_Integer
        (Read (F, Buffer.all'Address, Integer (Length)));
      Close (F);

      Insert (Widget, Chars => Buffer.all);
   end;




More information about the gtkada mailing list