Standard streams
≈ 8 min read
Names in other languages
No names in other languages are held for this entry yet.
Article bodies are English and are not machine-translated. A name in another language is not a translation of the article.
In computer programming, standard streams are preconnected input and output communication channels between a computer program and its environment when it begins execution. The three input/output (I/O) connections are called standard input (stdin), standard output (stdout) and standard error (stderr). Originally I/O happened via a physically connected system console (input via keyboard, output via monitor), but standard streams abstract this. When a command is executed via an interactive shell, the streams are typically connected to the text terminal on which the shell is running, but can be changed with redirection or a pipeline. More generally, a child process inherits the standard streams of its parent process.
In computer programming, standard streams are preconnected input and output communication channels between a computer program and its environment when it begins execution. The three input/output (I/O) connections are called standard input (stdin), standard output (stdout) and standard error (stderr). Originally I/O happened via a physically connected system console (input via keyboard, output via monitor), but standard streams abstract this. When a command is executed via an interactive shell, the streams are typically connected to the text terminal on which the shell is running, but can be changed with redirection or a pipeline. More generally, a child process inherits the standard streams of its parent process.
Standard input (stdin)
Standard input is a stream from which a program reads its input data. The program requests data transfers by use of the read operation. Not all programs require stream input. For example, the dir and ls programs (which display file names contained in a directory) may take command-line arguments, but perform their operations without any stream data input. Unless redirected, standard input is inherited from the parent process.
In the case of an interactive shell, that is usually associated with the input device of a terminal (or pseudo terminal) which is ultimately linked to a user's keyboard. On POSIX systems, the file descriptor for standard input is 0 (zero); the POSIX definition is STDIN_FILENO ; the corresponding C abstraction is provided via the stdin (of type FILE* ) global variable. Similarly in C++, the global object std::cin (of type std::istream ). provided in , provides an abstraction via C++ streams.
Similar abstractions exist in the standard I/O libraries of practically every programming language.
Standard output (stdout)
Standard output is a stream to which a program writes its output data. The program requests data transfer with the write operation. Not all programs generate output. For example, the file rename command (variously called mv, move, or ren) is silent on success. Unless redirected, standard output is inherited from the parent process. In the case of an interactive shell, that is usually the text terminal which initiated the program.
The file descriptor for standard output is 1 (one); the POSIX definition is STDOUT_FILENO ; the corresponding C variable is stdout (of type FILE* ); similarly in C++, the global object std::cout (of type std::ostream ), provided in , abstracts the output stream. include include using std::string; int main() { string input; std::cout > input; int inputLength = input.length(); std::cout In C++23, an updated printing interface was created for writing to the output stream, using std::print functions. import std; using std::string; int main() { string s = "Hello, world!"; std::println(stdout, "My string: {}", s); std::println(stderr, "String to error stream: {}", s); } Fortran has the equivalent of Unix file descriptors: By convention, many Fortran implementations use unit numbers UNIT=5 for stdin, UNIT=6 for stdout and UNIT=0 for stderr.
In Fortran 2003, the intrinsic ISO_FORTRAN_ENV module was standardized to include the named constants INPUT_UNIT , OUTPUT_UNIT , and ERROR_UNIT to portably specify the unit numbers. ! FORTRAN 77 example PROGRAM MAIN INTEGER NUMBER READ(UNIT=5,*) NUMBER WRITE(UNIT=6,'(A,I3)') ' NUMBER IS: ',NUMBER END !
Fortran 2003 example program main use iso_fortran_env implicit none integer :: number read (unit=INPUT_UNIT,*) number write (unit=OUTPUT_UNIT,'(a,i3)') 'Number is: ', number end program package org.wikipedia.examples; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Scanner; public class Example { public static void main(String args[]) { try { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String s = br.readLine(); double number = Double.parseDouble(s); System.out.printf("Number is: %d%n", number); // Read input for a name and age: Scanner input = new Scanner(System.in); System.out.printf("%nEnter name: "); String name = input.nextLine(); System.out.printf("%nEnter age: "); int age = input.nextInt(); System.out.printf("Hello, %s!
You are %d years old.", name, age); } catch (IOException e) { System.err.printf("Error in input/output: %s%n", e.getMessage()); } catch (Exception e) { System.err.printf("Error: %s%n", e.getMessage()); } } } class BASICIO (LINELENGTH); integer LINELENGTH; begin ref (infile) SYSIN; ref (infile) procedure sysin; sysin :- SYSIN; ref (printfle) SYSOUT; ref (printfle) procedure sysout; sysout :- SYSOUT; class FILE ....................; FILE class infile ............; FILE class outfile ...........; FILE class directfile ........; outfile class printfle .......; SYSIN :- new infile ("SYSIN"); SYSOUT :- new printfle ("SYSOUT"); SYSIN.open (blanks(80)); SYSOUT.open(blanks(LINELENGTH)); inner; SYSIN.close; SYSOUT.close; end BASICIO; namespace Wikipeda.Examples; using System; public class Example { static int Main(string[] args) { try { string s = Console.In.ReadLine(); double number = Double.Parse(s); Console.Out.WriteLine("Number is: {0:F3}", number); } // If Parse() threw an exception catch (ArgumentNullException e) { Console.Error.WriteLine($"No number was entered: {e.Message}"); return 1; } catch (FormatException e) { Console.Error.WriteLine($"The specified value is not a valid number: {e.Message}"); return 2; } catch (OverflowException e) { Console.Error.WriteLine($"The specified number is too big: {e.Message}"); return 3; } catch (Exception ex) { Console.Error.WriteLine($"An unknown exception occurred: {e.Message}"); return -1; } return 0; } } In Visual Basic .NET: Public Function Main() As Integer Try Dim s As String = System.Console.[In].ReadLine() Dim number As Double = Double.Parse(s) System.Console.Out.WriteLine("Number is: {0:F3}", number) Return 0 ' If Parse() threw an exception Catch ex As System.ArgumentNullException System.Console.[Error].WriteLine("No number was entered!") Catch ex2 As System.FormatException System.Console.[Error].WriteLine("The specified value is not a valid number!") Catch ex3 As System.OverflowException System.Console.[Error].WriteLine("The specified number is too big!") End Try Return -1 End Function When applying the System.Diagnostics.Process class one can use the instance properties StandardInput , StandardOutput , and StandardError of that class to access the standard streams of the process. !/usr/bin/env python import sys from typing import TextIO if __name__ == "__main__": # Save the current stdout so that we can revert sys.stdout # after we complete our redirection stdin_fileno: TextIO = sys.stdin stdout_fileno: TextIO = sys.stdout # Redirect sys.stdout to the file sys.stdout: TextIO = open("myfile.txt", "w") ctr: int = 0 for inps in stdin_fileno: ctrs: str = str(ctr) # Prints to the redirected stdout () sys.stdout.write(f"{ctrs}) this is to the redirected --->{inps}\n") # Prints to the actual saved stdout handler stdout_fileno.write(f"{ctrs}) this is to the actual --->{inps}\n") ctr = ctr + 1 # Close the file sys.stdout.close() # Restore sys.stdout to our old saved file handler sys.stdout = stdout_fileno
GUIs
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 the Synaptic package manager GUI, which wraps apt commands in Debian and/or Ubuntu. GUIs created with scripting tools like Zenity and KDialog by KDE project make use of stdin, stdout, and stderr, and are based on simple scripts rather than a complete GUI programmed and compiled in C/C++ using Qt, GTK, or other equivalent proprietary widget framework.
The Services menu, as implemented on NeXTSTEP and Mac 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 current selection 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 emulators pSX and DOSBox. GTK-server can use stdin as a communication interface with an interpreted program to realize a GUI. The Common Lisp Interface Manager paradigm "presents" GUI elements sent to an extended output stream.
See also
Redirection Stream Input/output C file input/output SYSIN and SYSOUT Standard streams in the Files-11 file system
References
External links
Standard Input Definition - by The Linux Information Project Standard Output Definition - by The Linux Information Project Standard Error Definition - by The Linux Information Project
Text derived from https://en.wikipedia.org/w/index.php?oldid=1377665505 under CC BY-SA 4.0; modifications have been made.