From: "Nick Roberts" <nick.roberts@acm.org>
Subject: Re: reading a text file into a string
Date: Thu, 15 Jul 2004 20:57:36 +0100
Date: 2004-07-15T20:57:36+01:00 [thread overview]
Message-ID: <opsa622anap4pfvb@bram-2> (raw)
In-Reply-To: mailman.24.1089913767.416.comp.lang.ada@ada-france.org
On Thu, 15 Jul 2004 18:49:17 +0100, Marius Amado Alves
<amado.alves@netcabo.pt> wrote:
> Unbounded_String is the right container for this. But note Get
> for characters skips over newlines and relatives (I think!) Do
> you really want to loose that information? If not then consider
> using Get_Immediate or Get_Line. This assuming you are using
> Ada.Text_IO.
I don't suggest using Get_Immediate for this purpose. You could use:
(a) a combination of Get and the End_of_Line function; or
(b) Get_Line.
Option (a) is likely to be slower, but this might not worry you.
If you use option (b), you must either not care about lines which are too
long (or know that none are) or you must write special code to deal with
overlong lines.
Example of option (a):
c, char: character;
text : unbounded_string;
Line_Break: constant Character := Ada.Characters.Latin_1.NUL;
...
-- read in text file
while not End_of_File(File) loop
if End_of_Line(File) then
Append( text, Line_Break );
Skip_Line(File);
end if;
Get( File, c );
Append( text, c );
end loop;
...
-- display content of unbounded_string:
for i in 1..Length(text) loop
if Element(text,i) = Line_Break then
Put_Line;
else
Put( Element(text,i) );
end if;
end loop;
-- process text
for i in 1 .. length ( text ) loop
char := element ( text, i );
...
end loop;
This will work, but it may not be very efficient.
Example of option (b):
with AI302.Containers.Vectors;
with Ada.Strings.Unbounded;
use Ada.Strings.Unbounded;
...
package Line_Vectors is
new AI302.Containers.Vectors(Positive,Unbounded_String);
use Line_Vectors;
...
Text: Line_Vectors.Vector_Type;
Line: String(1..100);
LL: Natural;
...
-- Read in text file:
while not End_of_File(File) loop
Read( File, Line, LL ); -- read line or line segment
Append( Text, To_Unbounded_String(Line(1..LL)) );
end loop;
-- Display content of unbounded_string:
for i in 1..Natural(Length(Text)) loop
Put_Line( To_String( Element(Text,i) ) );
end loop;
...
If a line is in the file which is longer than 100 characters, it will be
broken into two or more lines in the Text variable. Try this yourself.
You can download the AI-302 sample implementation packages from:
http://home.earthlink.net/~matthewjheaney/charles/ai302-20040227.zip
courtesy of Matthew Heaney (thanks Matt).
--
Nick Roberts
next prev parent reply other threads:[~2004-07-15 19:57 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 [this message]
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
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