Using global standard streams

Restriction: This section does not apply to AMODE 64.

In the default inheritance model, the behavior of C standard streams is such that a child main() function cannot affect the standard streams of the parent. The child can use the parent's definition or redirect a standard stream to a new location, but when control returns to the parent, the standard stream reverts back to the definition of the parent. In the global model, the C standard streams, stdin, stdout, and stderr, can be redirected to a different location while running in a child main() function and have that redirection stay in effect when control returns to the parent. You can use the _EDC_GLOBAL_STREAMS environment variable to set standard stream behavior to the global model. For more information, see _EDC_GLOBAL_STREAMS.

Table 1 highlights the standard stream behavior differences between the default inheritance model and the global model.
Table 1. Standard stream behavior differences
Behavior Default Inheritance Model Global Model
POSIX(OFF) Standard streams are opened automatically on first reference. (Same)
POSIX(ON) Standard streams are opened during initialization of the process, before the application receives control. Not supported.
default open modes As described in Default open modes. (Same)
default locations As described in this topic. (Same)
command line redirection Changes the location for the main being called and subsequent child programs. Changes the location for the entire C environment.
direct assignment Affects the current main and subsequent child programs. Affects the current main only. This definition is not passed on to a subsequent child program. The child gets the current global definition, if there is one defined.
freopen() Changes location for the main from which it is called and affects any subsequent child programs. Changes location for the entire C environment.
MSGFILE() runtime option Redirects stderr for the main being invoked and affects any subsequent child programs. When control returns to a parent program, stderr reverts back to the definition of the parent. If stderr is also redirected on the command line, that redirection takes precedence. (Same)
fclose() Closes the standard stream in current main only. Closes the standard stream for the entire C environment. The standard stream cannot be global anymore. Only direct assignment can be used to use the standard stream, and that would only be for the main in which it is assigned.
file position and visible data As currently described in this topic. File position and visible data across mains are as if there were only one main. No special processing occurs during the ANSI system() call. The standard streams are left untouched. When either entering or returning from a child program, reading or writing to the standard streams begin where previously left off,
C++ I/O Stream

cin defaults to stdin
cout defaults to stdout
cerr defaults to stderr (unbuffered)
clog defaults to stderr (buffered)

(Same)
Notes:
  1. The following environments do not allow global standard stream behavior as an option:
    • POSIX(ON)
    • CICS
    • SPC
    • AMODE 64
  2. You must identify the behavior of the standard streams to the C runtime library before initialization of the first C main in the environment. The default behavior uses the inheritance model. Once you set the standard stream behavior, it cannot be changed. Attempts to change the behavior after the first C main has been initialized are ignored.
  3. The value of the environment variable, when queried, does not necessarily reflect the standard stream behavior being used. This is because the value of the environment variable can be changed after the standard stream behavior has been set.
  4. The behaviors described in Table 1 only apply to the standard streams that use the global behavior.