1950s: Fortran 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. PROGRAM MAIN INTEGER NUMBER READ(UNIT=5,*) NUMBER WRITE(UNIT=6,'(A,I3)') ' NUMBER IS: ',NUMBER END 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
1960: ALGOL 60 ALGOL 60 was criticized for having no standard file access.
1968: ALGOL 68 ALGOL 68's input and output facilities were collectively referred to as the transput.
Koster coordinated the definition of the
transput standard. The model included three standard channels: stand in, stand out, and stand back.
1968: Simula An other example is the OOP language. 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;
1970s: C and Unix In the
C programming language, the standard input, output, and error streams are attached to the existing Unix file descriptors 0, 1 and 2 respectively. In a
POSIX environment the '''' definitions , or should be used instead rather than
magic numbers. File pointers , , and are also provided.
Ken Thompson (designer and implementer of the original Unix operating system) modified
sort in
Version 5 Unix to accept "-" as representing standard input, which spread to other utilities and became a part of the operating system as a
special file in
Version 8. Diagnostics were part of standard output through
Version 6, after which
Dennis M. Ritchie created the concept of standard error.
1990s: C++, Java In
C++, writing to standard streams was originally done using the header and its streams, until the release of which simplified input/output using print functions. C++ inherits
C I/O facilities, but it is considered more idiomatic to use the newer C++ facilities. • include • include using std::cerr; using std::cin; using std::cout; using std::endl; using std::string; int main() { string input; cout > input; int inputLength = input.length(); cout In
Java, the standard streams are referred to by (for stdin), (for stdout), and (for stderr). It is also possible to read from any input stream using a . package org.wikipedia.examples; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; 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()); } } }
2000s: .NET In
C# and other
.NET languages, the standard streams are referred to by System.Console.In (for stdin), System.Console.Out (for stdout) and System.Console.Error (for stderr). Basic read and write capabilities for the stdin and stdout streams are also accessible directly through the class System.Console (e.g. System.Console.WriteLine() can be used instead of System.Console.Out.WriteLine()). System.Console.In, System.Console.Out and System.Console.Error are respectively System.IO.TextReader (stdin) and System.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 the System.IO.Stream objects returned by System.Console.OpenStandardInput(), System.Console.OpenStandardOutput() and System.Console.OpenStandardError() respectively. 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; } ' Visual Basic .NET example 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.
2000s onward: Python, C++ The following example, written in
Python, shows how to redirect the standard input both to the standard output and to a text file. • !/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 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); }
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==