[gtkada] text files

Preben Randhol randhol at pvv.org
Mon Nov 12 19:01:43 CET 2001


> Hi!
> I'm having this problem time ago, I'm trying to do a 
> program that can create a text file and can load
> a text file too. In Ada I used to do this:
> 
>         c: character;
>         file: file_type;
>         begin
>         while not end_of_file(file) loop
>                 while not end_of_line(file) loop
>                         get(file,c);
>                         put(c);
>                 end loop;
>                 new_line;
>         end loop;
>         end;

Why don't you use Get_Line instead?

> 
> and this works fine.
> The problem cames when I try to do this using Gtk,
> I use this procedure to save the file:
> 
> procedure save (b: access gtk.button.gtk_button_record'class) is
>       name: string(1..gtk.text.get_chars(txt)'length);--> file's name
>       info: string(1..gtk.text.get_chars(texto)'length);--> I can't use a
>        character type?
>       file: file_type;
> begin --guardar
>       name:=gtk.text.get_chars(txt);-->txt is a gtk_text;
>       create(file, name=>name);
>       info:=gtk.text.get_chars(text);-->text is a gtk_text
>       put(file, info);
>       close(file);
> end save;
> 
> and this one to load the file:
>       
> procedure load (b: access gtk.button.gtk_button_record'class) is
>       name:string(1..gtk.text.get_chars(txt)'length);
>       file: file_type;
>       c: string(1..1); --> any suggestions?
> begin --cargar
>       name:=gtk.text.get_chars(txt);
>       open(fichero, in_file, name=>name);
>       while not end_of_file(file) loop
>               while not end_of_line(file) loop
>                get(file,c);
>                gtk.text.insert(texto, chars=>c);
>               end loop;

You miss new_line; here

>        c(1):=character'VAL(10); ------------>HERE DOES THE PROBLEM BEGIN!!
>        gtk.text.insert(texto, chars=>c);---->
>       end loop;
>       close(file);
> end cargar;

You can also do sth like this, depending on what your needs are:

   procedure Read (File_Name  : in     String;) is

      Max_Line_Length         : constant Integer := 255;

      Line                    : String(1..Max_Line_Length);
      Word_Length             : Integer;

   begin

      Open (File => Word_File, Mode => In_File, Name => File_Name);

      while not End_Of_File (File => Word_File) loop

         Get_Line (File => Word_File, Item => Line, Last => Word_Length);

      end loop;

      Close (File => Word_File);

   end Read;



Preben
-- 
Preben Randhol ------------------- http://www.pvv.org/~randhol/ --
                 «For me, Ada95 puts back the joy in programming.»




More information about the gtkada mailing list