comp.lang.ada
 help / color / mirror / Atom feed
From: "Steve" <nospam_steved94@comcast.net>
Subject: Re: reading a text file into a string
Date: Fri, 16 Jul 2004 02:26:44 GMT
Date: 2004-07-16T02:26:44+00:00	[thread overview]
Message-ID: <E1HJc.101277$Oq2.96646@attbi_s52> (raw)
In-Reply-To: 40f6bf21@dnews.tpgi.com.au

Sorry for the blank reply (obese finger)

The easiest way to read an entire file into a string, if you're looking for
speed, is to
use Ada.Direct_Io;

Here is a small working example:

with Ada.Direct_Io;
with Ada.Text_Io;

procedure Demo is

    function Get_File_Size( file_name : String ) return Natural is
        package Direct_Io_Char_File is new Ada.Direct_IO( Character );
        use Direct_Io_Char_File;
        size_file : File_Type;
        result : Natural;
    begin
        Open( size_file, In_File, file_name );
        result := Natural( Size( size_file ) );
        Close( size_file );
        return result;
    end Get_File_Size;

begin
    declare
        File_Size : Natural := Get_File_Size( "demo.txt" );
        subtype File_String is String( 1 .. File_Size );
        package File_Reader is new Ada.Direct_Io( File_String );
        in_file : File_Reader.File_Type;
        file_data : File_String;
    begin
        File_Reader.Open( in_file, File_Reader.In_File, "demo.txt" );
        File_Reader.Read( in_file, file_data );
        File_Reader.Close( in_file );
        -- Do what you will with file_data
        Ada.Text_Io.Put( file_data );
    end;
end Demo;

The basic idea is to first create an instance of direct_io for characters,
just to use the
"size" function to find out how many characters are in the file.  Then
create an instance
of direct_io for a string of the same length as the file, and do one read.

This gives you the content of the file as one raw string.  Which may or may
not be
what you want, but it is what you asked for.

Steve
(The Duck)

"zork" <zork@nospam.com> wrote in message news:40f6bf21@dnews.tpgi.com.au...
> hi, i would like to read a whole text file into a string. I thought of
using
> an unbounded_string for this:
>
> ----------
> c, char: character;
> text : unbounded_string;
> ...
> -- read in text file
> while not end_of_file ( File ) loop
>     Get ( File, c );
>     append ( text, c );
> end loop;
> ...
> put ( to_string ( text ) ); -- display content of unbounded_string
>
> -- process text
> for i in 1 .. length ( text ) loop
>   char := element ( text, i );
>   ...
> end loop;
> -----------
>
> ... is this the general way of going about it? or is there a more prefered
> method of reading in a whole text file (into whatever format) for
> processing?
>
> Thanks again!
>
> cheers,
> zork
>
>





  parent reply	other threads:[~2004-07-16  2:26 UTC|newest]

Thread overview: 44+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-07-15 17:27 reading a text file into a string zork
2004-07-15 17:49 ` Marius Amado Alves
2004-07-15 19:57   ` Nick Roberts
2004-07-15 17:59 ` Marius Amado Alves
2004-07-15 19:18   ` Nick Roberts
2004-07-15 19:18 ` Nick Roberts
2004-07-15 20:02   ` Nick Roberts
2004-07-16  1:23 ` Jeffrey Carter
2004-07-16  2:20 ` Steve
2004-07-16  2:26 ` Steve [this message]
2004-07-16 16:16   ` Jeffrey Carter
2004-07-16 17:45     ` Nick Roberts
2004-07-16 21:19   ` Randy Brukardt
2004-07-17  2:27     ` Robert I. Eachus
2004-07-17 11:31       ` Mats Weber
2004-07-17 15:52         ` Robert I. Eachus
2004-07-17 22:38           ` Jeffrey Carter
2004-07-18 13:44             ` zork
2004-07-19  8:07       ` Dale Stanbrough
2004-07-19  8:58         ` Martin Dowie
2004-07-21  0:17           ` Robert I. Eachus
2004-07-21 21:39             ` Randy Brukardt
2004-07-22 22:34               ` Robert I. Eachus
2004-07-23  0:49                 ` Randy Brukardt
2004-07-23 21:56                   ` Nick Roberts
2004-07-24  0:34                     ` tmoran
2004-07-24  1:16                       ` Nick Roberts
2004-07-24  1:42                     ` Randy Brukardt
2004-07-24 15:14                       ` Nick Roberts
2004-07-26 23:48                         ` Randy Brukardt
2004-07-27 12:08                           ` Nick Roberts
2004-07-27 23:24                             ` Robert I. Eachus
2004-07-29  0:55                               ` Randy Brukardt
2004-07-29  0:53                             ` Randy Brukardt
2004-07-29  7:25                               ` Martin Dowie
2004-07-29 20:08                               ` Robert I. Eachus
2004-07-30  0:14                                 ` tmoran
2004-07-24  2:56                   ` Robert I. Eachus
2004-07-19 11:51       ` Ada2005 (was " Peter Hermann
2004-07-19 12:51         ` Dmitry A. Kazakov
2004-07-19 13:01         ` Nick Roberts
2004-07-19 13:35           ` Martin Dowie
2004-07-19 17:22             ` Nick Roberts
2004-07-19 23:50           ` Randy Brukardt
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox