tmcclory@valhalla.cs.wright.edu (Tom McClory) (01/27/91)
A few novice questions on using Text_Io ... I've checked a couple of "intro" text books on Ada, and the LRM, but I haven't found a solution to either of these (trivial?) problems. I would welcome suggestion from any gurus in netland. If there is sufficient interest, I'll post a summary. 1) How to open a text file to append to an existing text file? 2) Is there some way to write to either Standard_Output or to a file? The Text_Io.File_Type is limited private, so the following doesn't work: The_File : File_Type; The_File := Standard_Output; ... Put(The_File, "hello world"); I then tried the following but the exception "Name_Error" was raised: Open( File => The_File, Mode => In_Out, Name => Name(Standard_Output)); I also tried Create, but this created a new file in my directory named "stdout". Thanks, Tom McClory tmcclory@cs.wright.edu Wright State Univ. Dayton, OH.
mfeldman@seas.gwu.edu (Michael Feldman) (01/29/91)
In article <1991Jan26.185319.15676@cs.wright.edu> tmcclory@valhalla.cs.wright.edu (Tom McClory) writes: >A few novice questions on using Text_Io ... > >1) How to open a text file to append to an existing text file? Not specified in LRM. Some vendors use the FORM parameter in the OPEN statement, but this is not portable as not everyone does. Ada9x is looking closely at this one. > >2) Is there some way to write to either Standard_Output or to a file? Have a look at SET_INPUT and SET_OUTPUT in the file section of Text_IO. (This'll be in the LRM for sure). Mike Feldman --------------------------------------------------------------------------- Prof. Michael Feldman Department of Electrical Engineering and Computer Science The George Washington University Washington, DC 20052 U.S.A. phone 202-994-5253 fax 202-994-5296 email mfeldman@seas.gwu.edu ---------------------------------------------------------------------------
stt@inmet.inmet.com (01/30/91)
Re: Text-IO questions. 1) Open with append There is a semi-standard Form string of "APPEND=>YES" which several compilers recognize to get open with append. I think is what the Posix/Ada binding proposed. To be completely portable, you generally have to create a temp file, copy the contents of the old file into the new file, and then write your new data onto the end of the temp file. When all done you copy the temp file back on top of the old file. One final mechanism is to write just your new data to a temp file, and then call a Unix.System("cat temp_file >> old_file") subprogram that some vendors provide. 2) Standard Output or other file As far as having a file which is either Standard_Output or some other file, you can use the function Current_Output instead, which is initially Standard_Output, but may be changed by a Set_Output call. If this is not flexible enough, you can write your own function Switchable_Output, which takes a File_Type and returns it if Is_Open returns True, and returns Standard_Output otherwise. For example: function Switchable_Output(File : File_Type) return File_Type is begin if Is_Open(File) then return File; else return Standard_Output; end if; end Switchable_Output; S. Tucker Taft Intermetrics, Inc. Cambridge, MA 02138 P.S. Text-IO has been identified in the Ada 9X requirements document as requiring additional functionality, in particular the open-with-append capability.
eachus@aries.mitre.org (Robert I. Eachus) (01/30/91)
In article <1991Jan26.185319.15676@cs.wright.edu> tmcclory@valhalla.cs.wright.edu (Tom McClory) writes:
A few novice questions on using Text_Io ...
-- Actually one easy, one not....
1) How to open a text file to append to an existing text file?
-- Check the Appendix F supplied with your compiler. If it supports
-- opening files in append mode, it should tell you what entry to put
-- in the FORM string to do it. Unfortunately, this string is
-- implementation specific, so I usually use a literal to make the
-- code more portable:
APPEND := constant String := "<whatever>";
...
Text_Io.Open(Foo, Text_IO.Out_File, Name => "Bar", Form => Append);
2) Is there some way to write to either Standard_Output or to a file?
-- You can't OPEN, CLOSE, DELETE, or RESET, the standard input and
-- output files because these procedures take in out parameters.
-- However, the other I/O functions are quite happy to take the value
-- returned by Standard_Output as a parameter. But you want to write
-- a section of code which writes something either to a named file or
-- to the standard output file? Sound like a procedure with a
-- parameter of Text_IO.File_Type. You should probably have an
-- exception handler or code to deal with cases where the file passed
-- is not open:
procedure Write_Message (To: in Text_IO.File_Type, Message: in String) is
Was_Closed: Boolean := not IS_OPEN(To)
begin
if Was_Closed
then
begin
OPEN(To, TEXT_IO.Out_File, "Junk");
exception
-- what goes here depends on how careful you need to be.
end;
end if;
Text_IO.Put_Line(" Junk_Message: " & Message);
end Write_Message;
--
Robert I. Eachus
Our troops will have the best possible support in the entire
world. And they will not be asked to fight with one hand tied behind
their back. President George Bush, January 16, 1991
eachus@aries.mitre.org (Robert I. Eachus) (01/30/91)
In article <1991Jan26.185319.15676@cs.wright.edu> tmcclory@valhalla.cs.wright.edu (Tom McClory) writes:
A few novice questions on using Text_Io ...
-- Actually one easy, one not....
1) How to open a text file to append to an existing text file?
-- Check the Appendix F supplied with your compiler. If it supports
-- opening files in append mode, it should tell you what entry to put
-- in the FORM string to do it. Unfortunately, this string is
-- implementation specific, so I usually use a literal to make the
-- code more portable:
APPEND := constant String := "<whatever>";
...
Text_Io.Open(Foo, Text_IO.Out_File, Name => "Bar", Form => Append);
2) Is there some way to write to either Standard_Output or to a file?
-- You can't OPEN, CLOSE, DELETE, or RESET, the standard input and
-- output files because these procedures take in out parameters.
-- However, the other I/O functions are quite happy to take the value
-- returned by Standard_Output as a parameter. But you want to write
-- a section of code which writes something either to a named file or
-- to the standard output file? Sound like a procedure with a
-- parameter of Text_IO.File_Type. You should probably have an
-- exception handler or code to deal with cases where the file passed
-- is not open:
procedure Write_Message (To: in Text_IO.File_Type, Message: in String) is
Was_Closed: Boolean := not IS_OPEN(To)
begin
if Was_Closed
then
begin
OPEN(To, TEXT_IO.Out_File, "Junk");
exception
-- what goes here depends on how careful you need to be.
end;
end if;
Text_IO.Put_Line(To," Junk_Message: " & Message);
end Write_Message;
--
Robert I. Eachus
Our troops will have the best possible support in the entire
world. And they will not be asked to fight with one hand tied behind
their back. President George Bush, January 16, 1991