by Dinesh Thakur Category: File Handling

The devices used with a computer, such as a keyboard, monitor, printer, hard disk, magnetic tape, etc., have widely varying properties regarding data input and output. To simplify data I/O operations, the C standard library supports a simple mode of input and output based on the concept of a stream.

A stream is a sequence of characters. There are two types of streams: text and binary. The sequence of characters in a text stream is composed into lines, i. e., a sequence of zero or more characters followed by a newline character. The newline character has no significance in a binary stream.

The various input and output devices have widely varying speed of data transfer. For example, a dot matrix printer with a typical speed of 300 cps (characters per second) is a very slow device, whereas a CD ROM drive at 52x speed has 150 x 52, i.e., 7800 KB/s speed. The CPU, on the other hand, can process data at a much higher speed. Thus, while performing I/O operations, the CPU must wait until the device involved in the data transfer . operation is ready for I/O operation. To avoid long delays and to free the CPU for other work, the concept of buffering is used, in which instead of performing I/O directly with a device, data is written to a memory buffer. Since memory is an electronic circuit, buffered I/O operations are very fast. A stream may be buffered, unbuffered or line buffered.

To understand how buffering works, first consider that a program transfers data to an output device. The CPU first writes the data to a buffer at a very high speed. Once the buffer is filled (or a newline is encountered in a text stream), the CPU stops the output operation and is free to do other jobs. The contents of the buffer are now sent to the device at the required speed by the associated hardware. Once the buffer is empty (i. e., all the data is sent to the output device), the CPU again fills the buffer with new output data.

The reverse happens when data is read from an input device. The data from the input device is first received in a buffer. During this, the CPU is free to perform other activities. Once the buffer is full (or a newline is encountered for a text file), the CPU quickly reads the data from the buffer and continues with program execution.

Built-in Streams

The primary devices used for user interaction are the keyboard and display. Almost every program performs some data input and output operations with these devices. Hence, the C language provides three built-in streams for performing I/O with these devices: stdin, stdout and stderr. Every C program has these three streams associated with it.

The standard input stream, stdin, is associated with the keyboard for performing input operations. The stdout and stderr streams are associated with the display. The stdout stream is used for the program output, whereas stderr is used for reporting errors.

About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.C.A, MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.