comp.lang.ada
 help / color / mirror / Atom feed
From: johnherro@aol.com (John Herro)
Subject: Re: simple problem?
Date: 1996/05/10
Date: 1996-05-10T00:00:00+00:00	[thread overview]
Message-ID: <4mvl9q$f1d@newsbf02.news.aol.com> (raw)
In-Reply-To: Pine.SGI.3.91.960509195810.8410A-100000@sgi17.york.ac.uk


Frank Cheung <fktc101@york.ac.uk> gets
an error compiling a program containing:
> package TEXT_IO is new SEQUENTIAL_IO(STRING);
     If you're using an Ada83 compiler, you must instantiate SEQUENTIAL_IO
with a *constrained* type or subtype.  STRING is an unconstrained type. 
In a moment I'm going to tell you that you don't want to instantiate
SEQUENTIAL_IO at all, but for instructional purposes, I'll show you how to
do it before we throw the idea away!
     First, there's already a TEXT_IO package in Ada, so, to avoid
confusion, you would want to use another name for your package.  How about
HOAX_IO?  You could write:
subtype Time_Of_Call_Subtype is STRING(1 .. 12);
package HOAX_IO is new SEQUENTIAL_IO(Time_Of_Call_Subtype);
use HOAX_IO;
and then you could write call times to your hoax file.  *OR* you could
write:
subtype Caller_Description_Subtype is STRING(1 .. 400);
package HOAX_IO is new SEQUENTIAL_IO(Caller_Description_Subtype);
use HOAX_IO;
and then you could write caller descriptions to your hoax file.
     The trouble is that you want to write *both* call times and caller
descriptions to the same file.  The package TEXT_IO that comes with Ada is
just right for that.  It's made for writing variable-length text records. 
So forget SEQUENTIAL_IO.  You want to with and use TEXT_IO rather than
instantiate SEQUENTIAL_IO.
     To correct your program, I made that change, and I created one
declaration of
CallFile : FILE_TYPE;
in the main program, rather than one in each subprogram, so that each
subprogram could write to the same file.  Because we're using TEXT_IO, I
changed every "write" to "Put_Line."  I made CREATE_HOAX_FILE create the
file and write the header, but not close the file, because
APPEND_HOAX_FILE will add to it.  Likewise, I took the open and close out
of APPEND_HOAX_FILE, and closed the file at the end of the main program. 
The procedure OUTPUT_HOAX_FILE is unnecessary, because, even without this
procedure, you've created the file, appended to it, and closed it. 
Finally I removed the unused declarations of Time_Of_Call and
Caller_Description in the main program.   Here's your corrected program:
with TEXT_IO; use TEXT_IO;
procedure MAIN is
   FileName : STRING(1..12) := "hoaxfile.dat";
   CallFile : FILE_TYPE;

   procedure CREATE_HOAX_FILE is
   begin
      Create(CallFile, NAME=>FileName);
      Put_Line(CallFile, "Hoax/Complaint file created on 9th May, 1996");
   end CREATE_HOAX_FILE;

   procedure APPEND_HOAX_FILE(Time_Of_Call, Caller_Description:STRING) is
   begin
      Put_Line(CallFile, Time_Of_Call);
      Put_Line(CallFile, Caller_Description);
   end APPEND_HOAX_FILE;
begin
   CREATE_HOAX_FILE;
   APPEND_HOAX_FILE("12:00", "Ben");
   APPEND_HOAX_FILE("12:15", "Adam");
   APPEND_HOAX_FILE("18:00", "Frank");
   Close(CallFile);
end MAIN;

Here's the output file hoaxfile.dat after your program is run:
Hoax/Complaint file created on 9th May, 1996
12:00
Ben
12:15
Adam
18:00
Frank

     Now, I suspect that you'll want to be able to append to the hoax file
later.  The best solution is to get an Ada95 compiler.  You would still
use Text_IO, but you would Open the hoax file in Append_File mode, which
isn't available in Ada83.  If you're stuck with an Ada83 compiler, the
best way to "append" to a text file with variable-length records is, at
the start of the program, to read the entire file a line at a time and
write each line to a new file.  When you get to the end of the input file,
close it, but leave the output file open for further writing.  At the end
of your program, delete the input file and rename the output file.  (The
Ada95 solution is easier!)
     Ada is a large language that can be a bit confusing for beginners,
but you'll find the people at comp.lang.ada eager to help you.   Please
don't hesitate to ask questions if any of this is unclear.
- John Herro
Software Innovations Technology
http://members.aol.com/AdaTutor
ftp://members.aol.com/AdaTutor




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

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1996-05-09  0:00 simple problem? Frank Cheung
1996-05-10  0:00 ` Robert I. Eachus
1996-05-10  0:00   ` Kevin D. Heatwole
1996-05-10  0:00 ` John English
1996-05-10  0:00   ` Robert A Duff
1996-05-13  0:00     ` John English
1996-05-13  0:00       ` Robert A Duff
1996-05-14  0:00       ` Keith Thompson
1996-05-10  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