Incomputer programming,standard streams are preconnected input and outputcommunication channels[1] between a computer program and its environment when it begins execution. The threeinput/output (I/O) connections are calledstandard input (stdin),standard output (stdout) andstandard error (stderr). Originally I/O happened via a physically connectedsystem console (input via keyboard, output via monitor), but standard streams abstract this. When a command is executed via an interactiveshell, the streams are typically connected to thetext terminal on which the shell is running, but can be changed withredirection or apipeline. More generally, achild process inherits the standard streams of itsparent process.
Users generally know standard streams as input and output channels that handle data coming from an input device, or that write data from the application. The data may be text with any encoding, orbinary data.When a program is run as adaemon, its standard error stream is redirected into a log file, typically for error analysis purposes.
Streams may be used to chain applications, meaning that the output stream of one program can be redirected to be the input stream to another application. In many operating systems this is expressed by listing the application names, separated by the vertical bar character, for this reason often called thepipeline character. A well-known example is the use of apagination application, such asmore, providing the user control over the display of the output stream on the display.
In most operating systems predatingUnix, programs had to explicitly connect to the appropriate input and output devices. OS-specific intricacies caused this to be a tedious programming task. On many systems it was necessary to obtain control of environment settings, access a local file table, determine the intended data set, and handle hardware correctly in the case of apunch card reader,magnetic tape drive,disk drive,line printer, card punch, or interactive terminal.
One of Unix's several groundbreaking advances wasabstract devices, which removed the need for a program to know or care what kind of devices it was communicating with[citation needed]. Older operating systems forced upon the programmer a record structure and frequentlynon-orthogonal data semantics and device control. Unix eliminated this complexity with the concept of a data stream: an ordered sequence of data bytes which can be read until theend of file. A program may also write bytes as desired and need not, and cannot easily declare their count or grouping.
Another Unix breakthrough was to automatically associate input and output to terminal keyboard and terminal display, respectively, by default[citation needed] — the program (and programmer) did absolutely nothing to establish input and output for a typical input-process-output program (unless it chose a different paradigm). In contrast, previous operating systems usually required some—often complex—job control language to establish connections, or the equivalent burden had to be orchestrated by the program.[citation needed]
Since Unix provided standard streams, the UnixC runtime environment was obliged to support it as well. As a result, most C runtime environments (andC's descendants), regardless of the operating system, provide equivalent functionality.
Standard input is a stream from which a program reads its input data. The program requests data transfers by use of theread operation. Not all programs require stream input. For example, thedir andls programs (which display file names contained in a directory) may takecommand-line arguments, but perform their operations without any stream data input.
Unlessredirected, standard input is inherited from the parent process. In the case of an interactive shell, that is usually associated with the input device of aterminal (orpseudo terminal) which is ultimately linked to a user'skeyboard.
OnPOSIX systems, thefile descriptor for standard input is 0 (zero); thePOSIX<unistd.h>
definition isSTDIN_FILENO
; the corresponding C<stdio.h>
abstraction is provided via theFILE* stdin
global variable. Similarly, the global C++std::cin
variable of type<iostream>
provides an abstraction viaC++ streams. Similar abstractions exist in the standard I/O libraries of practically everyprogramming language.
Standard output is a stream to which a program writes its output data. The program requests data transfer with thewrite operation. Not all programs generate output. For example, thefile rename command (variously calledmv,move, orren) is silent on success.
Unlessredirected, standard output is inherited from the parent process. In the case of an interactive shell, that is usually thetext terminal which initiated the program.
Thefile descriptor for standard output is 1 (one); thePOSIX<unistd.h>
definition isSTDOUT_FILENO
; the corresponding C<stdio.h>
variable isFILE* stdout
; similarly, the C++<iostream>
variable isstd::cout
.
Standard error is another output stream typically used by programs to outputerror messages or diagnostics. It is a stream independent of standard output and can be redirected separately.
This solves thesemi-predicate problem, allowing output and errors to be distinguished, and is analogous to a function returning a pair of values – seeSemipredicate problem § Multivalued return. The usual destination is thetext terminal which started the program to provide the best chance of being seen even ifstandard output is redirected (so not readily observed). For example, output of a program in apipeline is redirected to input of the next program or a text file, but errors from each program still go directly to the text terminal so they can be reviewed by the user in real time.
It is acceptable and normal to directstandard output andstandard error to the same destination, such as the text terminal. Messages appear in the same order as the program writes them, unlessbuffering is involved. For example, in common situations the standard error stream is unbuffered but the standard output stream is line-buffered; in this case, text written to standard error later may appear on the terminal earlier, if the standard output stream buffer is not yet full.
Thefile descriptor for standard error is defined byPOSIX as 2 (two); the<unistd.h> header file provides the symbolSTDERR_FILENO
;[2] the corresponding C<stdio.h>
variable isFILE* stderr
. The C++<iostream>
standard header provides two variables associated with this stream:std::cerr
andstd::clog
, the former being unbuffered and the latter using the same buffering mechanism as all other C++ streams.
Bourne-style shells allowstandard error to be redirected to the same destination that standard output is directed to using
2>&1
csh-style shells allowstandard error to be redirected to the same destination that standard output is directed to using
>&
Standard error was added to Unix in the 1970s after several wasted phototypesetting runs ended with error messages being typeset instead of displayed on the user's terminal.[3]
Fortran has the equivalent of Unix file descriptors: By convention, many Fortran implementations use unit numbersUNIT=5
for stdin,UNIT=6
for stdout andUNIT=0
for stderr. In Fortran-2003, the intrinsicISO_FORTRAN_ENV
module was standardized to include the named constantsINPUT_UNIT
,OUTPUT_UNIT
, andERROR_UNIT
to portably specify the unit numbers.
! FORTRAN 77 examplePROGRAMMAININTEGERNUMBERREAD(UNIT=5,*)NUMBERWRITE(UNIT=6,'(A,I3)')' NUMBER IS: ',NUMBEREND
! Fortran 2003 exampleprogrammainuseiso_fortran_envimplicit noneinteger::numberread(unit=INPUT_UNIT,*)numberwrite(unit=OUTPUT_UNIT,'(a,i3)')'Number is: ',numberend program
ALGOL 60 was criticized for having no standard file access.[citation needed]
ALGOL 68's input and output facilities were collectively referred to as the transput.[4]Koster coordinated the definition of thetransput standard. The model included three standard channels:stand in
,stand out
, andstand back
.
# ALGOL 68 example #main:( REAL number; getf(stand in,($g$,number)); printf(($"Number is: "g(6,4)"OR "$,number)); # OR # putf(stand out,($" Number is: "g(6,4)"!"$,number)); newline(stand out)) | |
Input: | Output: |
---|---|
3.14159 | Number is: +3.142 OR Number is: +3.142! |
In theC programming language, the standard input, output, and error streams are attached to the existing Unix file descriptors 0, 1 and 2 respectively.[5] In aPOSIX environment the<unistd.h> definitionsSTDIN_FILENO,STDOUT_FILENO orSTDERR_FILENO should be used instead rather thanmagic numbers. File pointersstdin,stdout, andstderr are also provided.
Ken Thompson (designer and implementer of the original Unix operating system) modifiedsort inVersion 5 Unix to accept "-" as representing standard input, which spread to other utilities and became a part of the operating system as aspecial file inVersion 8. Diagnostics were part of standard output throughVersion 6, after whichDennis M. Ritchie created the concept of standard error.[6]
InJava, the standard streams are referred to bySystem.in
(for stdin),System.out
(for stdout), andSystem.err
(for stderr).[7]
publicstaticvoidmain(Stringargs[]){try{BufferedReaderbr=newBufferedReader(newInputStreamReader(System.in));Strings=br.readLine();doublenumber=Double.parseDouble(s);System.out.println("Number is:"+number);}catch(Exceptione){System.err.println("Error:"+e.getMessage());}}
InC# and other.NET languages, the standard streams are referred to bySystem.Console.In
(for stdin),System.Console.Out
(for stdout) andSystem.Console.Error
(for stderr).[8] Basic read and write capabilities for the stdin and stdout streams are also accessible directly through the classSystem.Console
(e.g.System.Console.WriteLine()
can be used instead ofSystem.Console.Out.WriteLine()
).
System.Console.In
,System.Console.Out
andSystem.Console.Error
areSystem.IO.TextReader
(stdin) andSystem.IO.TextWriter
(stdout, stderr) objects, which only allow access to the underlying standard streams on a text basis. Full binary access to the standard streams must be performed through theSystem.IO.Stream
objects returned bySystem.Console.OpenStandardInput()
,System.Console.OpenStandardOutput()
andSystem.Console.OpenStandardError()
respectively.
// C# examplepublicstaticintMain(string[]args){try{strings=System.Console.In.ReadLine();doublenumber=double.Parse(s);System.Console.Out.WriteLine("Number is: {0:F3}",number);return0;// If Parse() threw an exception}catch(ArgumentNullException){System.Console.Error.WriteLine("No number was entered!");}catch(FormatException){System.Console.Error.WriteLine("The specified value is not a valid number!");}catch(OverflowException){System.Console.Error.WriteLine("The specified number is too big!");}return-1;}
' Visual Basic .NET examplePublicFunctionMain()AsIntegerTryDimsAsString=System.Console.[In].ReadLine()DimnumberAsDouble=Double.Parse(s)System.Console.Out.WriteLine("Number is: {0:F3}",number)Return0' If Parse() threw an exceptionCatchexAsSystem.ArgumentNullExceptionSystem.Console.[Error].WriteLine("No number was entered!")Catchex2AsSystem.FormatExceptionSystem.Console.[Error].WriteLine("The specified value is not a valid number!")Catchex3AsSystem.OverflowExceptionSystem.Console.[Error].WriteLine("The specified number is too big!")EndTryReturn-1EndFunction
When applying theSystem.Diagnostics.Process
class one can use the instancepropertiesStandardInput
,StandardOutput
, andStandardError
of that class to access the standard streams of the process.
The following example, written inPython, shows how to redirect the standard input both to the standard outputand to a text file.
#!/usr/bin/env pythonimportsys# Save the current stdout so that we can revert sys.stdout# after we complete our redirectionstdin_fileno=sys.stdinstdout_fileno=sys.stdout# Redirect sys.stdout to the filesys.stdout=open("myfile.txt","w")ctr=0forinpsinstdin_fileno:ctrs=str(ctr)# Prints to the redirected stdout ()sys.stdout.write(ctrs+") this is to the redirected --->"+inps+"\n")# Prints to the actual saved stdout handlerstdout_fileno.write(ctrs+") this is to the actual --->"+inps+"\n")ctr=ctr+1# Close the filesys.stdout.close()# Restore sys.stdout to our old saved file handlersys.stdout=stdout_fileno
Graphical user interfaces (GUIs) do not always make use of the standard streams; they do when GUIs are wrappers of underlying scripts and/or console programs, for instance theSynaptic package manager GUI, which wraps apt commands in Debian and/or Ubuntu. GUIs created with scripting tools like Zenity and KDialog byKDE project[9] make use of stdin, stdout, and stderr, and are based on simple scripts rather than a complete GUI programmed and compiled in C/C++ usingQt,GTK, or other equivalent proprietary widget framework.
TheServices menu, as implemented onNeXTSTEP andMac OS X, is also analogous to standard streams. On these operating systems, graphical applications can provide functionality through a system-wide menu that operates on the currentselection in the GUI, no matter in what application.
Some GUI programs, primarily on Unix, still write debug information to standard error. Others (such as many Unix media players) may read files from standard input. Popular Windows programs that open a separate console window in addition to their GUI windows are the emulatorspSX andDOSBox.
GTK-server can use stdin as a communication interface with an interpreted program to realize a GUI.
TheCommon Lisp Interface Manager paradigm "presents" GUI elements sent to an extended output stream.