All Topics  
Standard streams

 

   Email Print
   Bookmark   Link






 

Standard streams



 
 
In Unix
Unix

Unix is a computer operating system originally developed in 1969 by a group of American Telephone & Telegraph employees at Bell Labs, including Ken Thompson , Dennis Ritchie, Douglas McIlroy, and Joe Ossanna....
 and Unix-like
Unix-like

A Unix-like operating system is one that behaves in a manner similar to a Unix system, while not necessarily conforming to or being certified to any version of the Single UNIX Specification....
 operating systems, as well as certain programming language
Programming language

A programming language is a machine-readable artificial language designed to express computations that can be performed by a machine, particularly a computer....
 interfaces, the standard streams are preconnected input and output channels between a computer program and its environment (typically a text terminal) when it begins execution.






Discussion
Ask a question about 'Standard streams'
Start a new discussion about 'Standard streams'
Answer questions from other users
Full Discussion Forum



Encyclopedia


Stdstreams Notitle
In Unix
Unix

Unix is a computer operating system originally developed in 1969 by a group of American Telephone & Telegraph employees at Bell Labs, including Ken Thompson , Dennis Ritchie, Douglas McIlroy, and Joe Ossanna....
 and Unix-like
Unix-like

A Unix-like operating system is one that behaves in a manner similar to a Unix system, while not necessarily conforming to or being certified to any version of the Single UNIX Specification....
 operating systems, as well as certain programming language
Programming language

A programming language is a machine-readable artificial language designed to express computations that can be performed by a machine, particularly a computer....
 interfaces, the standard streams are preconnected input and output channels between a computer program and its environment (typically a text terminal) when it begins execution. The three I/O
Input/output

In computing, input/output, or I/O, refers to the communication between an information processing system , and the outside world ? possibly a human, or another information processing system....
 connections are called standard input, standard output and standard error.

Background

In most operating systems predating Unix, programs had to explicitly connect to the appropriate input and output data. On many of those systems, this could be an intimidating programming challenge created by OS-specific intricacies such as obtaining control environment settings, accessing a local file table, determining the intended data set, and handling the correct case of a card reader, magnetic tape drive, disk drive, line printer, card punch, or interactive terminal.

Unix provided several groundbreaking advances, one of which was to provide abstract devices: it removed the need for a program to know or care what kind of devices it was communicating with. Older operating systems forced upon the programmer a record structure and, frequently non-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 the end of file
End-of-file

In computing, end-of-file, commonly abbreviated EOF, is a condition in a computer operating system where no more data can be read from a data source....
. A program may also write bytes as desired and need not (and can't easily) declare how many there will be, or how they will be grouped.

Another Unix breakthrough was to automatically associate input and output by default—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
Job Control Language

Job Control Language is a scripting language used on IBM mainframe operating systems to instruct the system on how to run a batch processing or start a subsystem....
 to establish connections, or the equivalent burden had to be orchestrated by the program.

Since Unix provided standard streams, the Unix C runtime environment was obligated to support it as well. As a result, most C runtime environments (and C's descendants), regardless of the operating system, provide equivalent functionality.

Standard input (stdin)

Standard input is data (often text) going into a program. The program requests data transfers by use of the read operation. Not all programs require input. For example, the dir or ls program (which displays file names contained in a directory) performs its operation without any stream data input.

Unless redirected, input is expected from the text keyboard which started the program.

The file descriptor
File descriptor

In computer programming, a file descriptor is an abstract key for accessing a file. The term is generally used in POSIX operating systems. In Microsoft Windows terminology and in the context of the stdio.h, "file handle" is preferred, though the latter case is technically a different object ....
 for standard input is 0 (zero); the POSIX
POSIX

POSIX or "Portable Operating System Interface" is the collective name of a family of related standardizations specified by the Institute of Electrical and Electronics Engineers to define the application programming interface , along with shell and utilities interfaces for software compatible with variants of the Unix operating system, altho...
  definition is STDIN_FILENO; the corresponding variable is FILE* stdin; similarly, the variable is stdcin.

Standard output (stdout)

Standard output is the stream where 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, ren) is silent on success.

Unless redirected
Redirection (Unix)

In computing, redirection is a function common to most command-line interpreters, including the various Unix shells that can redirect standard streams to user-specified locations....
, standard output is the text terminal which initiated the program.

The file descriptor
File descriptor

In computer programming, a file descriptor is an abstract key for accessing a file. The term is generally used in POSIX operating systems. In Microsoft Windows terminology and in the context of the stdio.h, "file handle" is preferred, though the latter case is technically a different object ....
 for standard output is 1 (one); the POSIX
POSIX

POSIX or "Portable Operating System Interface" is the collective name of a family of related standardizations specified by the Institute of Electrical and Electronics Engineers to define the application programming interface , along with shell and utilities interfaces for software compatible with variants of the Unix operating system, altho...
  definition is STDOUT_FILENO; the corresponding variable is FILE* stdout; similarly, the variable is stdcout.

Standard error (stderr)

Standard error is another output stream typically used by programs to output error message
Error message

An error message is a message displayed when an unexpected condition occurs, usually on a computer or other device. Error messages are often displayed using dialog boxes....
s or diagnostics. It is a stream independent of standard output and can be redirected separately. The usual destination is the text terminal which started the program to provide the best chance of being seen even if standard output is redirected (so not readily observed). For example, output of a program in a pipeline
Pipeline (Unix)

In Unix-like computer operating systems, a pipeline is the original pipeline : a set of process es chained by their standard streams, so that the output of each process feeds directly as input of the next one....
 is redirected to input of the next program, but errors from each program still go directly to the text terminal.

It is acceptable—and normal—for standard output and standard error to be directed to the same destination, such as the text terminal. Messages appear in the same order as the program writes them, unless buffering is involved. (For example, a common situation is when 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's buffer is not yet full.)

The file descriptor
File descriptor

In computer programming, a file descriptor is an abstract key for accessing a file. The term is generally used in POSIX operating systems. In Microsoft Windows terminology and in the context of the stdio.h, "file handle" is preferred, though the latter case is technically a different object ....
 for standard error is 2; the POSIX
POSIX

POSIX or "Portable Operating System Interface" is the collective name of a family of related standardizations specified by the Institute of Electrical and Electronics Engineers to define the application programming interface , along with shell and utilities interfaces for software compatible with variants of the Unix operating system, altho...
  definition is STDERR_FILENO; the corresponding variable is FILE* stderr. The C++ standard header provides two variables associated with this stream: stdcerr and stdclog, the former being unbuffered and the latter using the same buffering mechanism as all other C++ streams.

Most shells allow both standard output and standard error to be redirected to the same file using >& filename

Bourne-style shells allow standard error to be redirected to the same destination that standard output is directed to using 2>&1

Timeline


1950s: Fortran

Fortran had the equivalent of Unix file descriptors, UNIT=5 for stdin, and UNIT=6 for stdout. ! FORTRAN 77 example PROGRAM MAIN READ(UNIT=5,*)NUMBER WRITE(UNIT=6,'(F5.3)')' NUMBER IS: ',NUMBER END

1960: ALGOL 60

ALGOL 60 was criticized for having no standard file access.

1968: ALGOL 68

ALGOL 68
ALGOL 68

ALGOL 68 is an imperative programming computer programming programming language that was conceived as a successor to the ALGOL 60 programming language, designed with the goal of a much wider scope of application and more rigorously defined syntax and semantics....
's input and output facilities were collectively referred to as the transput. Koster
Cornelis H. A. Koster

Cornelis H.A. Koster is a professor in the Department of Informatics of the University of Nijmegen in the Netherlands.He was one of the editors of the original Report on the Algorithmic Language ALGOL 68....
  coordinated the definition of the transput standard. This standard included: stand in, stand out, stand error and stand back.

Example:
  1. ALGOL 68 example #
main); 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!


1970s: C and Unix

In the C programming language
C (programming language)

C is a general-purpose computer programming language originally developed in 1972 by Dennis Ritchie at the Bell Telephone Laboratories to implement the Unix operating system....
 stdin, stdout and stderr streams were attached to the existing Unix file descriptors 0, 1 and 2 respectively.

1995: Java

In Java
Java (programming language)

Java is a programming language originally developed by James Gosling at Sun Microsystems and released in 1995 as a core component of Sun Microsystems' Java ....
, the standard streams are referred to by (for stdin), (for stdout), and (for stderr). public static void main(String args[])

2000s: .NET

In C# and other .NET
.NET Framework

The Microsoft .NET Framework is a software framework that is available with several Microsoft Windows operating systems. It includes a large Library of coded solutions to prevent common programming problems and a virtual machine that manages the execution of programs written specifically for the Software framework....
 languages, the standard streams are referred to by System.Console (for stdin and stdout) and System.Console.Error (for stderr). // C# example public static int Main(string[] args)



' Visual Basic .NET example

Public Function Main As Integer Dim number As Double Dim s As String

Try s = System.Console.ReadLine number = CDbl(s) System.Console.WriteLine("Number is: ", number) Return 0 Catch e As System.InvalidCastException ' if CDbl threw an exception System.Console.Error.WriteLine("No number was entered!") Return 1 End Try End Function

When applying the System.Diagnostics.Process class
Class (computer science)

In object-oriented programming, a class is a programming language construct that is used as a blueprint to create Object s. This blueprint includes Attribute s and Method s that the created objects all share....
 one can use the instance properties
Property (programming)

In some object-oriented programming programming languages, a property is a special sort of Class member, intermediate between a field and a method ....
 StandardInput, StandardOutput, and StandardError of that class to access the standard streams of the process.

GUIs

Graphical user interface
Graphical user interface

A graphical user interface is a type of user interface which allows people to human-computer interaction such as computers; hand-held devices such as MP3 Players, Portable Media Players or Gaming devices; household appliances and office equipment....
s (GUIs) rarely make use of the standard streams. Consequently, redirecting GUI programs or constructing a GUI pipeline is neither practical nor useful. The nearest analog is probably cutting (or copying) from one application and pasting into another. Since manual user operations are required, moving large numbers of pastes is not especially efficient. One notable exception is the dwm
DWM

The acronym DWM can stand for:*Desktop Window Manager - A compositing window manager included with Microsoft Windows*Deutsche Waffen und Munitionsfabriken...
 tiling window manager
Tiling window manager

In computing, a tiling window manager is a window manager with an organization of the screen into mutually non-overlapping frames, as opposed to the more popular approach of coordinate-based stacking of overlapping objects that tries to fully emulate the desktop metaphor....
, which displays data directed through stdin on a status bar.

Some GUI programs, primarily on Unix, still write debug information to standard error.

GTK-server
GTK-server

GTK-server is an open source project released under the GNU General Public License. The GTK-server project aims to bring Graphical User Interface programming to any interpreted language using the GIMP Tool Kit or XForms ....
 can use stdin as communication interface with an interpreted program to realize a GUI.

See also

  • Redirection (computing)
  • Pipeline (Unix)
    Pipeline (Unix)

    In Unix-like computer operating systems, a pipeline is the original pipeline : a set of process es chained by their standard streams, so that the output of each process feeds directly as input of the next one....
  • Stream (computing)
  • Input/output
    Input/output

    In computing, input/output, or I/O, refers to the communication between an information processing system , and the outside world ? possibly a human, or another information processing system....
  • C file input/output


External links

  • - by The Linux Information Project (LINFO)