From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,FREEMAIL_FROM autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,a3fe2aac201210c0 X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news1.google.com!news.glorb.com!wns13feed!worldnet.att.net!attbi_s52.POSTED!53ab2750!not-for-mail From: "Steve" Newsgroups: comp.lang.ada References: <40f6bf21@dnews.tpgi.com.au> Subject: Re: reading a text file into a string X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2800.1409 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1409 Message-ID: NNTP-Posting-Host: 24.21.42.251 X-Complaints-To: abuse@comcast.net X-Trace: attbi_s52 1089944804 24.21.42.251 (Fri, 16 Jul 2004 02:26:44 GMT) NNTP-Posting-Date: Fri, 16 Jul 2004 02:26:44 GMT Organization: Comcast Online Date: Fri, 16 Jul 2004 02:26:44 GMT Xref: g2news1.google.com comp.lang.ada:2199 Date: 2004-07-16T02:26:44+00:00 List-Id: 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" 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 > >