comp.lang.ada
 help / color / mirror / Atom feed
* Mixing reading and writing to a text file
@ 2007-08-02  7:43 msimonides
  2007-08-03  1:26 ` Randy Brukardt
  0 siblings, 1 reply; 3+ messages in thread
From: msimonides @ 2007-08-02  7:43 UTC (permalink / raw)


I'm facing a situation, where I need to access a text file for both
input and output.

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).

Currently I'm mixing Text_IO and Direct_IO: Text_IO is used for
reading and writing the file initially and Direct_IO is used to
overwrite the first dozen characters that specify state.
This approach has many deficiencies, of course, like reopening the
file in different mode (which might fail due to permissions).
My first question is: can I freely mix Text_IO and Direct_IO at all?

The main question is: How should I approach this problem?

I've considered using Ada/POSIX binding like Florist but I'm not sure
whether it would be portable to Windows (currently there's no such
requirement in our project, but I don't want to close this path).
--
Marcin Simonides




^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Mixing reading and writing to a text file
  2007-08-02  7:43 Mixing reading and writing to a text file msimonides
@ 2007-08-03  1:26 ` Randy Brukardt
  2007-08-06  7:35   ` msimonides
  0 siblings, 1 reply; 3+ messages in thread
From: Randy Brukardt @ 2007-08-03  1:26 UTC (permalink / raw)


<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.





^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Mixing reading and writing to a text file
  2007-08-03  1:26 ` Randy Brukardt
@ 2007-08-06  7:35   ` msimonides
  0 siblings, 0 replies; 3+ messages in thread
From: msimonides @ 2007-08-06  7:35 UTC (permalink / raw)


On 3 Sie, 03:26, "Randy Brukardt" <ra...@rrsoftware.com> wrote:
> <msimoni...@power.com.pl> wrote in message
[...]
> > 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.
[...]

Thanks for clarification. I wrote a simple test that opens an In_File
and then Resets it as Out_File and writing works as expected - data is
overwritten over the bytes that I wish to change and there is no
truncation (the compiler is GNAT GPL 2007).

(I have read A.8.2 File Management and only skimmed over description
of Ada.Streams.Stream_IO, so this has been mostly an RTFM issue :) ).
--
Marcin Simonides




^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2007-08-06  7:35 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-08-02  7:43 Mixing reading and writing to a text file msimonides
2007-08-03  1:26 ` Randy Brukardt
2007-08-06  7:35   ` msimonides

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