Next: Filenames encoding, Previous: Stream_IO, Up: The Implementation of Standard I/O
Section A.14 of the Ada Reference Manual allows implementations to provide a wide variety of behavior if an attempt is made to access the same external file with two or more internal files.
To provide a full range of functionality, while at the same time minimizing the problems of portability caused by this implementation dependence, GNAT handles file sharing as follows:
Use_Error
will be
raised. Note that a file that is not explicitly closed by the program
remains open until the program terminates.
When a program that opens multiple files with the same name is ported
from another Ada compiler to GNAT, the effect will be that
Use_Error
is raised.
The documentation of the original compiler and the documentation of the
program should then be examined to determine if file sharing was
expected, and `shared=xxx' parameters added to Open
and Create
calls as required.
When a program is ported from GNAT to some other Ada compiler, no special attention is required unless the `shared=xxx' form parameter is used in the program. In this case, you must examine the documentation of the new compiler to see if it supports the required file sharing semantics, and form strings modified appropriately. Of course it may be the case that the program cannot be ported if the target compiler does not support the required functionality. The best approach in writing portable code is to avoid file sharing (and hence the use of the `shared=xxx' parameter in the form string) completely.
One common use of file sharing in Ada 83 is the use of instantiations of Sequential_IO on the same file with different types, to achieve heterogeneous input-output. Although this approach will work in GNAT if `shared=yes' is specified, it is preferable in Ada to use Stream_IO for this purpose (using the stream attributes)