File, Stream standard I/O
File
A file is actually a region of memory space in physical storage devices like disk drives, tape drives, printers, monitors, etc. The huge amount of data can be stored in the form of files in
hard disks and auxiliary devices.
These files in two categories based on the way they are accessed:
High-level file : Also called as stream-oriented files. These files are accessed by the library functions provided in the standard library header files (like stdio.h).
Low-level files: Also known as system-oriented files. These files are accessed by the system calls that are provided by the operating system itself.
High- level files makes use of the library functions provided in the standard library that can be used by including the header file stdio.h in the program. Since, these library functions are common to all the operating systems, so the programs using these library functions are portable.
Stream
The C file system is designed in such a way that is can work with various devices including disks, printers, tapes, terminals, etc. Since each of these devices (or files) is different from each other, there need to be some level of abstraction between the programmer and the actual device (or file) being accessed. This abstraction is called stream.
Stream works as an interface between the programmer and the device (or file). The programmer need not have to worry about the inner details of device (file) he is using, because all the streams act similarly (they can be opened, closed, read from, written into, etc).
Therefore, it is easier to handle so many kinds of actual devices (or files) using streams. Streams provide a portable way of reading and writing with minor or no changes at all. Streams provide a cleaner, flexible and efficient mean of I/O.
What is the difference between a file and a stream?
A file is handled by associating a stream to it, and there are following differences between a file and a stream:
- Files are the actual devices (disk, printer, etc.) whereas streams are the logical devices (i.e., an abstract representation of a device).
- Different files (devices) behave differently but the streams handling different files (devices) behave similarly.
- Not all files have the same capabilities. The disk file differs from a printer in that a disk file can support random access while some printers cannot. But, all streams are the same. They are largely device independent. The same function that will write to a disk file can also write to a printer.
Types of Streams in C
There are two types of streams in C.
- Text streams
- Binary streams
Text streams
A text streams is a sequence of characters. It can be organized into lines terminated by ‘\n’. For last line,’\n’ is optional because this line can be terminated by an EOF (End Of File mark).
Binary Streams
A binary stream is a sequence by bytes having a one-to-one correspondence to the bytes in the actual device. That is, no character translation occurs as in the case of a text file. So, the number of bytes read (or written) is the same as the number of bytes on the actual device.
Since binary streams are a mere sequence of bytes, there is no mark to indicate the end of file (as EOF in text streams). The end of file is, therefore, determined by the size of the file.
What do we mean by saying that “Stream I/O is buffered”?
“Stream I/O is buffered”. That is to say a fixed-size “chunk” is read from (or written to) a file by using some temporary storage area (known as buffer). This lead to efficient I/O, but we must be careful about one thing:
The data written to a buffer does not appear in a file (or device) until the buffer is flushed or written out (\n does this flushing). Any abnormal exit of code can cause problems.
What is standard I/O
The standard I/O is the most flexible, easiest, useful, and commonly used way of performing I/O in C. It provides various types of operations. It works as a uniform portable interface to provide common functionality among different systems. For example, I/O (printf, scanf, gets, puts etc.) file handling (fopen, fwrite, fclose, fseek, ftell etc.) and memory management (malloc, calloc, realloc, free etc). For this purpose, a standard I/O library is provided, which has many categories of functions defined, and functions of a particular category are all declared in a particular header file for easy interfacing.
Standard Input/output handling
In C, any I/O device is treated as if it were a file – a keyboard file, a monitor file, a printer file, a disk file, etc. An abstract representation of each of these files is known as a stream. When a C program starts executing, three predefined streams are automatically opened. These streams are:
-
Stdin
The standard input, which is the file (or device) from which input is received. This file is keyboard by default, but can be redirected by the operating system to receive the input from a disk file or any other input device.
-
Stdout
The standard output, which is the file to which output is directed. This file is screen (or monitor) by default, but can be redirected by the operating system to printer, or disk file or any other output device.
-
Stderr
The standard error output, which is the file used to keep separate the error message from program’s output. If the output is directed towards a disk file, the standard error file can be screen (or monitor), so that the user could see the error messages only on the screen and the error messages do not go into the disk file.
These all streams use text as the method of I/O. stdin and stdout can be used with files. programs, I/O devices such as keyboard, console, etc. stderr always goes to the console or screen. The console is the default for the stdout and stderr. The keyboard is the default for stdin. For some operating systems (such as DOS), two more streams are opened automatically when the program starts executing. They are :
- Stdprn– the standard printer; which is the file (or device) to which output is directed. This file is by default the first parallel (printer) port.
- Stdaux– the standard auxiliary; which is the file (or device) to which output is directed. This file is by default the first serial port.
What is the difference between stdout and stderr?
By default, both the stdout and stderr write to the monitor. The difference between them comes in the following two cases:
1 When the output of one file is redirected to other file.
then if error message in first file is written to stdout: if some error message in a file (say abc.c) is written to stdout, as shown below:
# include<stdio.h> main( ) { …. if (some_error) { fprintf (stdout,“Error in the program . . . ! Check it ! “); /* the error message. */ exit ( 0 ) ; } . . . }
After running the above program successfully, suppose we get an executable file (say, abc.exe on DOS). Now, suppose we divert the output of executable file abc.exe (from monitor) to any other file (say, a disk file abc.txt) as input, by doing
abc>abc.txt
on the prompt.
Then, what happens is that, along with the normal output, the error message
Error in the program. . . ! Check it !
also goes as an unwanted and unnecessary input to the file abc.txt. You can confirm it by opening the file abc.txt (by issuing the command: edit abc.txt on prompt). But, we intended the file abc.txt only to print the output of the program and not the error messages. So this approach (of writing error messages to stdout) is not good in this case.
If error message is written to stderr : by changing the stdout to stderr in the above program, we get the new program as shown below :
#include<stdio.h> main ( ) { . . . if (some_error) { fprintf (stderr, “Error in the program . . . ! Check it ! “ ) ; /* the error message. */ exit ( 0 ) ; } . . .
again, after running the program and getting the abc.exe, we redirect the output of abc.exe to another file abc.txt by doing :
abc>abc.txt
on the prompt. Now, the error message
Error in the program . . . ! Check it !
is displayed on the screen ( i.e. the error message is not included in the file abc1 . txt ) and rest of the output goes to the file abc.txt (again, confirm it by edit abc.txt command on the prompt). This is the advantage of using stderr instead of stdout that the unwanted error messages are stopped being written to the specified file towards which we are redirecting the output.
- The method of writing
The second difference between stdout and stderr is the way they work. stdout does not write directly to the output device. Rather, it first writes to the buffer (which is a temporary storage area), and then flushes the buffer to send the output to the device.
The write to the stderr is unbuffered. That is, the information written to stderr is immediately sent to the output device, without being stored in the buffer and then waiting for the buffer to be flushed. That is why, when we write error messages to stdout, we use fflush(stdout) in order to write the output completely and to display the message without any delay.
Redirection
This how we override the UNIX (and DOS) default predefined I/O defaults. This is not part of C, but operating system dependent. We do redirection from the command line.
> : to redirect output (stdout) to a file. Used both for DOS and UNIX. So if we have a program, abc, that, by default, prints to the stdout (i.e., screen), then
abc>file1
will send the output of abc to a file, file1 (instead of screen).
< : to redirect stdin from a file to a program. Used for both DOS and UNIX. So if we are expecting input from the keyboard for a program xyz, we can read similar input from a file named file2
xyz<file2.
| : is a pipe, and is used in both DOS and UNIX. It puts standard output (stdout) from one program (say, prog1) to standard input (stdin) of another (say, prog2).
prog1|prog2
e.g. sending output (usually to console) of a program in UNIX directly to the printer :
prog|lpr
similarly, to sort the contents of a directory in DOS, we do :
dir|sort
we can also combine redirection operators to attain more flexibility and action. For example, on DOS prompt, the command
dir|sort>a.txt
will sort the contents of the current directory, and place the result in the file abc.txt. You can confirm it by opening the file abc.txt by issuing the command:
edit abc.txt
Please Comment, Share and like the post.
Similar Posts:
You may also like:
Discounts in Sales Invoices in Tally.ERP9 JavaScript Control Statements and Operators What is New in HTML5
Download Official TurboC IDE cum Compiler from here
What is the difference between a text file and a binary file DOS and UNIX?
How is newline character treated in C?
In text mode, newline character is internally translated into CR/LF combination before writing to a file. Similarly, while reading a file, the CR/LF combination on the secondary storage device (such as disk) is internally translated back to a newline character.
In binary mode, this translation is not done. So, the newline character ‘\n’ will be written only as a linefeed (LF), and while reading the CR/LF combination will be treated as two distinct characters (‘\r’ and ‘\n’).
[…] Similar Topics : Recursion in C Preprocessor Directives in C File, Stream and Standard I/O […]
[…] Handling in C Language File, Stream and Standard I/O Preprocessor Directives in […]