comp.lang.ada
 help / color / mirror / Atom feed
From: johnherro@aol.com (John Herro)
Subject: Re: THE OLD IN OUT, IN OUT
Date: 1996/06/05
Date: 1996-06-05T00:00:00+00:00	[thread overview]
Message-ID: <4p349u$h8b@newsbf02.news.aol.com> (raw)
In-Reply-To: 4ou662$951@fang.dsto.defence.gov.au


Barney Rubble <gsu@ssa.army.defence.gov.au> wrote:
> Can anyone help me with this problem?
> I'm trying to modify (i.e. read and write
> to) a text file. As far as I know, you can
> only open a file as only In_File or
> Out_File, and Out_File always creates
> a blank file.  I tried to open two copies
> of a file, one as an In_File and one as an
> Out_File, simultaneously, but it came up
> with a Use_Error when I tried to open
> the second.  Is there (an easy) way of
> doing this?
     You're right, there's no *easy* way to modify a text file, and the
Use_Error was to be expected.  In Ada 95 you can Open a text file in
Append_File mode.  In Ada 83, Append_File mode isn't available, but in
some implementations you can accomplish the same thing by using the Form
parameter.  If you have an Ada 83 compiler, you'll have to consult the
compiler documentation, as different implementations of Ada 83 aren't
consistent in their handling of (or even allowing) a Form parameter.
     However, so far I've only talked about *appending* to a file.  You
can't *directly* modify what's already written.  But there are several
techniques you can use.  First, if you have enough memory, you can read
the entire file into memory (possibly into a linked list), modify memory,
and then write the entire file out.  Or, your problem may permit reading
the file a chunk (say, 50 lines) at a time, modifying it, and then writing
the chunk to a new file, all in a loop.  At the end of your program, you
can delete the old file and rename the new.
     With some implementations of Ada (for example, Open Ada for DOS), you
*could* open a text file with Direct_IO instantiated for Character, and do
random reads and writes a character at a time.  It's inefficient and
messy, but I've done it.  With other implementations of Ada (for example,
Vax Ada for VMS), this won't work, because files opened with an
instantiation of Direct_IO have different attributes from text files.
     In my Ada Tutor program, available for download at the WWW and FTP
sites below my signature, there's an example of a simple line editor that
works by reading the entire file into a linked list, modifying it, and
then writing out the entire file.
     I don't think this is what you were hoping to hear, but I hope it's
better than nothing.  Let us know if we can be of further help.
- John Herro
Software Innovations Technology
http://members.aol.com/AdaTutor
ftp://members.aol.com/AdaTutor




      reply	other threads:[~1996-06-05  0:00 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1996-06-03  0:00 THE OLD IN OUT, IN OUT Barney Rubble
1996-06-05  0:00 ` John Herro [this message]
replies disabled

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