comp.lang.ada
 help / color / mirror / Atom feed
From: "Randy Brukardt" <randy@rrsoftware.com>
Subject: Re: Mixing reading and writing to a text file
Date: Thu, 2 Aug 2007 20:26:33 -0500
Date: 2007-08-02T20:26:33-05:00	[thread overview]
Message-ID: <f8u03t$bhm$1@jacob-sparre.dk> (raw)
In-Reply-To: 1186040606.842876.191850@57g2000hsv.googlegroups.com

<msimonides@power.com.pl> wrote in message
news:1186040606.842876.191850@57g2000hsv.googlegroups.com...
> I'm facing a situation, where I need to access a text file for both
> input and output.

The only way to read and write the same file is using Ada.Streams.Stream_IO.
Use Reset or Set_Mode to change from Reading to Writing and vice versa.

> This file is used for storing a sequence of different records (of
> various lengths - it's not possible to define the upper bound)
> defining operations that need to be performed. Some errors could occur
> and the processing might be aborted. In this case it is crucial to
> store state of the processing (in the simplest case only the number of
> the last record processed) must be saved, so that it may be resumed
> later.
>
> The task seems simple as I have complete control over the file format.
> I'd rather use human-readable format but a binary one is also
> acceptable.
>
> The problem I have is with writing the state information back to file.
> Ada.Text_IO and Ada.Streams.Stream_IO only allow opening file for
> input or for output. In the latter case the file is being truncated
> (as far as I understand the ARM).

The ARM was screwed up in Ada 95 vis-a-vis Ada.Streams.Stream_IO. This was
fixed in the Amendment (and the fix is supposed to apply to Ada 95 compilers
as well). Specifically, stream files are *not* truncated when they are
opened for output (otherwise it would be virtually impossible to use the
positioning functions to write a stream file).

But a warning: almost all compilers got this wrong when we tested them while
we were working on the Amendment. (The main reason that we were willing to
change it was that virtually every compiler tested did something different.)
So it is not impossible that your implementation gets this wrong in some
way. But if it does, that is a compiler bug, not a language issue. Report it
to your implementer.

Also note that you don't need to Open the file to change the mode of a
stream file; Set_Mode should do the job. And even the buggy Ada 95 manual
didn't imply that Set_Mode should truncate, so that ought to work (but
again, not all compilers get this right - unfortunately, there was no ACATS
test for it).

                                   Randy.





  reply	other threads:[~2007-08-03  1:26 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-08-02  7:43 Mixing reading and writing to a text file msimonides
2007-08-03  1:26 ` Randy Brukardt [this message]
2007-08-06  7:35   ` msimonides
replies disabled

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