C Language / Lesson 7 | Back to Index or to Previous Page or to Next Page |
Files and Devices
In the C standard library there are two basic functions which read and write
respectively from/to the user's terminal :
getchar() returns one character every time it is called from the so called standard input
putchar(c) sends c to the standard output.
In most environments a file may be substituted for the terminal using the symbol
> for the output and < for the input. If the program myprog for example
uses getchar() to read from the terminal then the command
myprog < infile
will read input from infile instead of the keyboard.
This is called redirection.
The keyboard and the screen which are the peripheral devices
related to the above standard input and standard output (logical) devices respectively
plus two or three other standard devices which are explained below are treated
for the first time under UNIX (and of course "C") and later under DOS and other
console oriented operating systems as files. Although file functions are not
discussed here it must be mentioned that when opening a file in C using one
of the two existing standard functions a File Descriptor is
returned. This is a value which is used as the reference to the opened file.
The functions previously mentioned getchar() and putchar() are actually file
functions which read and write respectively from two special files opened automatically
when a program starts.
In other words these standard devices are treated as files. This is a great
feature since it is related with other concepts which give a great flexibity
to a programmer's strategic. For example a program made to write text to a file
can be used to write to the standard printer file i.e. to print instead of storing
the text on disk. Here is a table showing all the standard devices and other
important names related to them. It must be mentioned that UNIX and DOS do not
support all common features of standard devices.
- there are differences in redirection :
UNIX commands can redirect the error device also
- the name at the command line are not the same (here DOS names
are shown)
Device
|
Name in C
(ANSI descriptor) |
DOS Device
|
Mode
|
System file descriptor
|
Keyboard | stdin | CON | Input |
0
|
Screen | stdout | CON | Output |
1
|
Error | stderr | ( N/A) | Output |
2
|
T/C port | stdaux | COM1 | I/O |
3
|
Printer | stdprn | LPT1 | Output |
4
|
A function called getc( fd) similar to getchar() reads one character
from file fd everytime it is called.
This means that getchar() is identical to getc(stdio)
Comments
COM1 and LPT1 are names which are used also as file names, in
which case they are not used as standard devices.
The ANSI descriptors are pointers of type FILE (a special structure
predefined in C) . So while file pointers are used to access files (or devices)
using ANSI file functions , we can see that using system file functions the
descriptors are non-negative integers.
We call such a file pointer a stream since using the basic file functions
which read or write a character everytime other functions read/write a sequence
of characters for example one line terminated with CarriageReturn and
LineFeed