comp.lang.ada
 help / color / mirror / Atom feed
From: "Robert I. Eachus" <rieachus@comcast.net>
Subject: Re: reading a text file into a string
Date: Fri, 16 Jul 2004 22:27:03 -0400
Date: 2004-07-16T22:27:03-04:00	[thread overview]
Message-ID: <fOednXzORfHlE2Xd4p2dnA@comcast.com> (raw)
In-Reply-To: <LNOdnQjer4Hf22XdRVn-sA@megapath.net>

There have been a lot of useful tips in this thread on how to accomplish 
the stated goal.  But what is really missing is a discussion of HOW a 
newbie should decide what he actually wants to do.

What you have to do is refine your requirements, and that can be the 
most important, and most time consuming step when programming in Ada.

I usually state it as Ada is much better at doing what you tell it to do 
than other languages.  But it is like a four-year old child, always 
asking, "Why?"

So before you decide whether to represent line-breaks with nulls, 
linefeeds, or copy the existing characters exactly, you have to know the 
answer to the "Why?" question.  In this case, "Why are you reading the 
file?"

Once you know whether you need a bitwise copy of the file, to parse the 
text and reformat it, or merely to scan through the contents of the 
file, then you can decide the right way to read the file.  I usually 
find that when I have though about it enough, I want to do line at a 
time processing, rather than character at a time, or reading the entire 
file in one gulp.

For this reason, I find myself contructing or using a Get_Line FUNCTION 
inside a loop and a declare block:

while not End_of_Line(Somefile) loop
   declare
      Buffer: String := Get_Line(Somefile);
   begin
      -- process buffer
   exception
      ...
   end;
end loop;

Each iteration of the loop, the Buffer contains a CONSTANT String, but 
it is potentially different in length and content every time through.

Incidently, GNAT has a special package to allow you to do a Get_Line 
into an Unbounded_String, no matter how long.  I think I posted a 
"clever example" of how to do it here, and if you need it I can find it 
again.  (The code is an elegant example of the use of recursion.  Using 
the GNAT equivalent is better performance-wise if you really are reading 
multi-megabyte lines.)

-- 

                                           Robert I. Eachus

"The flames kindled on the Fourth of July, 1776, have spread over too 
much of the globe to be extinguished by the feeble engines of despotism; 
on the contrary, they will consume these engines and all who work them." 
-- Thomas Jefferson, 1821




  reply	other threads:[~2004-07-17  2:27 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
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 [this message]
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