* simple problem? @ 1996-05-09 0:00 Frank Cheung 1996-05-10 0:00 ` John Herro ` (2 more replies) 0 siblings, 3 replies; 9+ messages in thread From: Frank Cheung @ 1996-05-09 0:00 UTC (permalink / raw) Hello! When I try to compile the code below, I get the following error: "/usr/Ada/lib/sequential_i.A", line 67: error: generic formal private type element_type has unconstrained Can anyone help me out with this? The code is as follows: with SEQUENTIAL_IO; procedure MAIN is package TEXT_IO is new SEQUENTIAL_IO(STRING); use TEXT_IO; FileName : STRING(1..12) := "hoaxfile.dat"; Time_Of_Call : STRING(1..12); Caller_Description : STRING(1..400); procedure CREATE_HOAX_FILE is CallFile : FILE_TYPE; begin create(CallFile, NAME=>FileName); write(CallFile, "Hoax/Complaint file created on 9th May, 1996"); close(CallFile); end CREATE_HOAX_FILE; procedure OUTPUT_HOAX_FILE is CallFile : FILE_TYPE; begin open(CallFile, IN_FILE, FileName); while not END_OF_FILE(CallFile) loop read(CallFile, Time_Of_Call); read(CallFile, Caller_Description); end loop; close(CallFile); end OUTPUT_HOAX_FILE; procedure APPEND_HOAX_FILE(Time_Of_Call, Caller_Description:STRING) is CallFile : FILE_TYPE; begin open(CallFile, OUT_FILE, FileName); write(CallFile, Time_Of_Call); write(CallFile, Caller_Description); close(CallFile); 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"); end; Any comments/suggestions would be appreciated. Thanks. Frank. ________________Frank Cheung | Computer Science (BSc) Year 2_______________ / fktc101@york.ac.uk | 0881-800800 a/c 841276 | +44-1904-430000 ext 4250 \ |James College, University of York, Heslington, Y01 5DD, York, United Kingdom| ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: simple problem? 1996-05-09 0:00 simple problem? Frank Cheung @ 1996-05-10 0:00 ` John Herro 1996-05-10 0:00 ` Robert I. Eachus 1996-05-10 0:00 ` John English 2 siblings, 0 replies; 9+ messages in thread From: John Herro @ 1996-05-10 0:00 UTC (permalink / raw) 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 ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: simple problem? 1996-05-09 0:00 simple problem? Frank Cheung 1996-05-10 0:00 ` John Herro @ 1996-05-10 0:00 ` Robert I. Eachus 1996-05-10 0:00 ` Kevin D. Heatwole 1996-05-10 0:00 ` John English 2 siblings, 1 reply; 9+ messages in thread From: Robert I. Eachus @ 1996-05-10 0:00 UTC (permalink / raw) John gave a very nice answer to this question, however he also said: > 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 83 does not prohibit opening a Text_IO file for appending. In fact it provides a mechanism, the Form string, for requesting append mode. Many Ada 83 compilers support this. Unfortunately, the appropriate Form string is implementation defined, so check your vendor supplied appendix F. (Sorry to waste your time discussing obsolete languages. ;-) -- Robert I. Eachus with Standard_Disclaimer; use Standard_Disclaimer; function Message (Text: in Clever_Ideas) return Better_Ideas is... ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: simple problem? 1996-05-10 0:00 ` Robert I. Eachus @ 1996-05-10 0:00 ` Kevin D. Heatwole 0 siblings, 0 replies; 9+ messages in thread From: Kevin D. Heatwole @ 1996-05-10 0:00 UTC (permalink / raw) In article <EACHUS.96May10162546@spectre.mitre.org>, eachus@spectre.mitre.org (Robert I. Eachus) wrote: > Ada 83 does not prohibit opening a Text_IO file for appending. In >fact it provides a mechanism, the Form string, for requesting append >mode. Many Ada 83 compilers support this. Unfortunately, the >appropriate Form string is implementation defined, so check your >vendor supplied appendix F. I might add that any Ada 83 compiler that supports the Posix Ada standard (IEEE 1003.5-1992) uses the same Form string syntax to specify appending. I think most compilers support this standard (on platforms where it makes sense). Kevin Heatwole OC Systems, Inc. ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: simple problem? 1996-05-09 0:00 simple problem? Frank Cheung 1996-05-10 0:00 ` John Herro 1996-05-10 0:00 ` Robert I. Eachus @ 1996-05-10 0:00 ` John English 1996-05-10 0:00 ` Robert A Duff 2 siblings, 1 reply; 9+ messages in thread From: John English @ 1996-05-10 0:00 UTC (permalink / raw) Frank Cheung (fktc101@york.ac.uk) wrote: : When I try to compile the code below, I get the following error: : "/usr/Ada/lib/sequential_i.A", line 67: error: : generic formal private type element_type has unconstrained : package TEXT_IO is new SEQUENTIAL_IO(STRING); The problem is that String is an unconstrained type; without knowing how big it is you can use Sequential_IO with it. Either use a constrained string (e.g. String(1..80) or some such) or use Text_IO.Put_Line (i.e. write a text file rather than a sequential file). -- ---------------------------------------------------------------------------- John English <je@brighton.ac.uk>, Dept. of Computing, University of Brighton "Disks are divided into sex and tractors..." ---------------------------------------------------------------------------- ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: simple problem? 1996-05-10 0:00 ` John English @ 1996-05-10 0:00 ` Robert A Duff 1996-05-13 0:00 ` John English 0 siblings, 1 reply; 9+ messages in thread From: Robert A Duff @ 1996-05-10 0:00 UTC (permalink / raw) In article <Dr6tqL.217@bton.ac.uk>, John English <je@bton.ac.uk> wrote: >Frank Cheung (fktc101@york.ac.uk) wrote: >: When I try to compile the code below, I get the following error: >: "/usr/Ada/lib/sequential_i.A", line 67: error: >: generic formal private type element_type has unconstrained > >: package TEXT_IO is new SEQUENTIAL_IO(STRING); > The problem is that String is an unconstrained type; ... Sequential_IO allows unconstrained arrays. Direct_IO does not. Other posters are correct that you probably want to be using the predefined Text_IO package, and certainly declaring your own thing called Text_IO, different from the predefined version, is bad form. - Bob ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: simple problem? 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 0 siblings, 2 replies; 9+ messages in thread From: John English @ 1996-05-13 0:00 UTC (permalink / raw) Robert A Duff (bobduff@world.std.com) wrote: : In article <Dr6tqL.217@bton.ac.uk>, John English <je@bton.ac.uk> wrote: : >Frank Cheung (fktc101@york.ac.uk) wrote: : >: package TEXT_IO is new SEQUENTIAL_IO(STRING); : > The problem is that String is an unconstrained type; ... : Sequential_IO allows unconstrained arrays. Direct_IO does not. Umm, this guy appears to me to be talking Ada 83 (which didn't allow instantiation of Sequential_IO with an unconstrained type), not 95. Just little giveaways like the "all caps" style and TEXT_IO rather than Ada.Text_IO. I may of course be wrong, and he might in fact be using Ada 95, in which case my answer *would* be complete rubbish, but then he wouldn't be getting this particular error, ne c'est pas? -- ---------------------------------------------------------------------------- John English <je@brighton.ac.uk>, Dept. of Computing, University of Brighton "Disks are divided into sex and tractors..." ---------------------------------------------------------------------------- ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: simple problem? 1996-05-13 0:00 ` John English @ 1996-05-13 0:00 ` Robert A Duff 1996-05-14 0:00 ` Keith Thompson 1 sibling, 0 replies; 9+ messages in thread From: Robert A Duff @ 1996-05-13 0:00 UTC (permalink / raw) In article <DrCsG5.K46@bton.ac.uk>, John English <je@bton.ac.uk> wrote: >Umm, this guy appears to me to be talking Ada 83 (which didn't allow >instantiation of Sequential_IO with an unconstrained type), not 95. Ada 83, IMHO, *did* allow it. Perhaps some implementations didn't. Ada 95 tried to clarify, in this case, what was already intended. Maybe the Ada 93 RM didn't make it clear, and maybe some implementations don't agree, and maybe the ACVC didn't enforce it. >Just little giveaways like the "all caps" style and TEXT_IO rather >than Ada.Text_IO. I may of course be wrong, and he might in fact be >using Ada 95, in which case my answer *would* be complete rubbish, but >then he wouldn't be getting this particular error, ne c'est pas? You're probably right. Although it's hard for some to give up the ALL_CAPS style. ;-) - Bob P.S. I certainly didn't mean to offend... ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: simple problem? 1996-05-13 0:00 ` John English 1996-05-13 0:00 ` Robert A Duff @ 1996-05-14 0:00 ` Keith Thompson 1 sibling, 0 replies; 9+ messages in thread From: Keith Thompson @ 1996-05-14 0:00 UTC (permalink / raw) In <DrCsG5.K46@bton.ac.uk> je@bton.ac.uk (John English) writes: [...] > Umm, this guy appears to me to be talking Ada 83 (which didn't allow > instantiation of Sequential_IO with an unconstrained type), not 95. Ada 83 doesn't specifically forbid instantiation of Sequential_IO (or Direct_IO, for that matter) with an unconstrained type. Whether it's actually allowed depends on the implementation. There's at least one Ada 83 implementation that allows Sequential_IO(String) but rejects Direct_IO(String). -- Keith Thompson (The_Other_Keith) kst@thomsoft.com <*> TeleSoft^H^H^H^H^H^H^H^H Alsys^H^H^H^H^H Thomson Software Products 10251 Vista Sorrento Parkway, Suite 300, San Diego, CA, USA, 92121-2718 This sig uses the word "Exon" in violation of the Communications Decency Act. ^ permalink raw reply [flat|nested] 9+ messages in thread
end of thread, other threads:[~1996-05-14 0:00 UTC | newest] Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 1996-05-09 0:00 simple problem? Frank Cheung 1996-05-10 0:00 ` John Herro 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
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox