* Random Access in Ada.Text_IO files
@ 2003-02-11 18:34 Micha� Morawski
2003-02-11 18:58 ` Stephen Leake
2003-02-11 21:03 ` Jeffrey Carter
0 siblings, 2 replies; 5+ messages in thread
From: Micha� Morawski @ 2003-02-11 18:34 UTC (permalink / raw)
Hi,
I have encountered the following problem
My program (one task) constantly writes some information to the file (log).
This is simple text line produced every 2 seconds.
Sometimes, other task try to analyze the log and the task need to read the
same file.
This works if files are opened in the "shared=" form. But of course both
tasks use different file position pointers. I wrote the simple test program
presented below. If shared is in "no" mode - I receive use_errer, If "yes"
mode, the file "aaaaaaa" is destroyed by Get (Ascii.null is inserted). After
I wrote the simple monitor to separate the operarions I must write either
extension of Ada.Text_IO or own module. In the first case I cannot compile
the module (I receive the message - it is the system module and cannot be
compiled), and my own module have no acces to stream field of File_Type.
Can anyone give me some advice?
procedure TF is
NAZWA_PLIKU : constant String := "aaaaaaa";
task Wr is
end Wr;
task body Wr is
F : File_Type;
ch : Character := 'a';
begin
Create (F, Out_File, NAZWA_PLIKU, "shared=no");
loop
Put (F, ch);
ch := Character'Succ (ch);
if ch > 'z' then
ch := 'a';
end if;
delay 0.01;
end loop;
exception
when others =>
Put_Line ("Blad w zadaniu");
end Wr;
P : File_Type;
ch : Character;
begin
delay 3.0;
Open (P, In_File, NAZWA_PLIKU, "shared=no");
Reset (P);
loop
Get (P, ch);
Put (ch);
delay 0.1;
end loop;
exception
when E : others =>
Put (Ada.Exceptions.Exception_Information (E) &
GNAT.Traceback.Symbolic.Symbolic_Traceback (E));
end TF;
--
Michal Morawski
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Random Access in Ada.Text_IO files
2003-02-11 18:34 Random Access in Ada.Text_IO files Micha� Morawski
@ 2003-02-11 18:58 ` Stephen Leake
2003-02-11 21:03 ` Jeffrey Carter
1 sibling, 0 replies; 5+ messages in thread
From: Stephen Leake @ 2003-02-11 18:58 UTC (permalink / raw)
"Micha� Morawski" <morawski@zsk.p.lodz.pl> writes:
> Sometimes, other task try to analyze the log and the task need to read the
> same file.
This is very operating system dependent. There is no guarantee that
Ada.Text_IO files will allow this.
If the two tasks are part of the same program, have them share an
in-memory queue of strings, rather than an external file.
> <snip> and my own module have no acces to stream field of
> File_Type.
If you are writing your own file package, you should not try build it
on top of Text_IO.
You might be able to use Ada.Streams.Stream_IO instead.
--
-- Stephe
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Random Access in Ada.Text_IO files
2003-02-11 18:34 Random Access in Ada.Text_IO files Micha� Morawski
2003-02-11 18:58 ` Stephen Leake
@ 2003-02-11 21:03 ` Jeffrey Carter
2003-02-12 9:11 ` Egil Harald H�vik
1 sibling, 1 reply; 5+ messages in thread
From: Jeffrey Carter @ 2003-02-11 21:03 UTC (permalink / raw)
In general, what you're trying to do won't work. Ada.Text_IO is intended
for sequential use only. Files are not the appropriate mechanism for
intertask communication; it would be better to use a protected object of
some sort.
Some general Ada concepts you might find useful:
Micha� Morawski wrote:
> procedure TF is
>
> NAZWA_PLIKU : constant String := "aaaaaaa";
>
> task Wr is
> end Wr;
This can be written using the special form
task Wr;
>
> task body Wr is
> F : File_Type;
> ch : Character := 'a';
> begin
> Create (F, Out_File, NAZWA_PLIKU, "shared=no");
> loop
> Put (F, ch);
> ch := Character'Succ (ch);
> if ch > 'z' then
> ch := 'a';
> end if;
> delay 0.01;
> end loop;
Might I suggest something along the lines of
subtype Lower is Character range 'a' .. 'z';
begin
Create (File => F, ...);
loop
for Ch in Lower loop
Put (File => F, Item => Ch);
delay 0.01;
end loop;
end loop;
> exception
> when others =>
> Put_Line ("Blad w zadaniu");
> end Wr;
--
Jeff Carter
"You've got the brain of a four-year-old boy,
and I bet he was glad to get rid of it."
Horse Feathers
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2003-02-12 9:42 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-02-11 18:34 Random Access in Ada.Text_IO files Micha� Morawski
2003-02-11 18:58 ` Stephen Leake
2003-02-11 21:03 ` Jeffrey Carter
2003-02-12 9:11 ` Egil Harald H�vik
2003-02-12 9:42 ` Egil Harald H�vik
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox