Standard Files
When a C program begins its execution, it has to access three files.
Ø Standard input file
Ø Standard output file
Ø Standard error file
Standard input file: The first file is the standard input file from which the input is received, usually it is a keyboard. Input can also be redirected from a disk file. As long as the program reads the input from the beginning to its end, it need not be concerned about where the characters are coming from.
standard output file: The second file is the standard output file to which the output is sent; usually it is the visual display unit (i.e. screen). Output can also be redirected to store into the disk file. As long as the program reads the input from the beginning to its end, it need not be concerned about where the characters are coming from.
standard error file: The third file is the standard error file. The purpose of this file is to keep error messages separate from other program output (i.e. if the standard output is displayed in the screen) so that the error messages are stored in the disk file and vice-versa.
For example
@ The statement x=fgetc(stdin); accepts a single character as input from the standard input device (i.e. keyboard) and assigns it to the variable x.
@ The statement fputc(x, stdout); displays the character as output on the standard output device (i.e screen) which is assigned to the variable x.
@ The statement fputc(x, stderr); also displays the character as output on the screen which is assigned to x. Even though the VDU is the output and error device, there are differences these two files.
When a C program begins its execution, it has to access three files.
Ø Standard input file
Ø Standard output file
Ø Standard error file
Standard input file: The first file is the standard input file from which the input is received, usually it is a keyboard. Input can also be redirected from a disk file. As long as the program reads the input from the beginning to its end, it need not be concerned about where the characters are coming from.
standard output file: The second file is the standard output file to which the output is sent; usually it is the visual display unit (i.e. screen). Output can also be redirected to store into the disk file. As long as the program reads the input from the beginning to its end, it need not be concerned about where the characters are coming from.
standard error file: The third file is the standard error file. The purpose of this file is to keep error messages separate from other program output (i.e. if the standard output is displayed in the screen) so that the error messages are stored in the disk file and vice-versa.
- The stdin, stdout, and stderr are file type pointers defined in the header file stdio.h.
- The standard input device (keyboard), standard output and error device (VDU) are called as the stdin, stdout, and stderr respectively. These file type pointers are treated as files in C.
- File related operations can be performed on these files just as other ordinary files.
For example
@ The statement x=fgetc(stdin); accepts a single character as input from the standard input device (i.e. keyboard) and assigns it to the variable x.
@ The statement fputc(x, stdout); displays the character as output on the standard output device (i.e screen) which is assigned to the variable x.
@ The statement fputc(x, stderr); also displays the character as output on the screen which is assigned to x. Even though the VDU is the output and error device, there are differences these two files.