Q. Can stdout be forced to print somewhere other than the screen?
Although the stdout standard stream defaults to the screen, you can force it to print to another device using something called redirection. For instance, consider the following program:
/* redir.c */
#include <stdio.h>
voidmain(
void);
voidmain(
void)
{
printf(
"Let's get redirected!\n");
}
At the DOS prompt, instead of entering just the executable name, follow it with the redirection character >, and thus redirect what normally would appear on-screen to some other device. The following example would redirect the program's output to the prn device, usually the printer attached on LPT1:
C:>REDIR > PRN
Alternatively, you might want to redirect the program's output to a file, as the following example shows:
C:>REDIR > REDIR.OUT
In this example, all output that would have normally appeared on-screen will be written to the file REDIR.OUT.
Q. What is the difference between text and binary modes?
Streams can be classified into two types: text streams and binary streams. Text streams are interpreted, with a maximum length of 255 characters. With text streams, carriage return/line feed combinations are translated to the newline \n character and vice versa. Binary streams are uninterpreted and are treated one byte at a time with no translation of characters. Typically, a text stream would be used for reading and writing standard text files, printing output to the screen or printer, or receiving input from the keyboard.
A binary text stream would typically be used for reading and writing binary files such as graphics or word processing documents, reading mouse input, or reading and writing to the modem.
Q. How do you determine whether to use a stream function or a low-level function?
Stream functions such as fread() and fwrite() are buffered and are more efficient when reading and writing text or binary data to files. You generally gain better performance by using stream functions rather than their unbuffered low-level counterparts such as read() and write().
In multiuser environments, however, when files are typically shared and portions of files are continuously being locked, read from, written to, and unlocked, the stream functions do not perform as well as the low- level functions. This is because it is hard to buffer a shared file whose contents are constantly changing.
Generally, you should always use buffered stream functions when accessing nonshared files, and you should always use the low-level functions when accessing shared files.
der:nm � m o pqj ج~ op-alt:solid gray .75pt; padding:3.75pt 3.75pt 3.75pt 3.75pt'> -
Standard Input
-
Keyboard
stdout
-
Standard Output
-
Screen
stderr
-
Standard Error
-
Screen
stdprn
-
Standard Printer
-
LPT1: port
stdaux
-
Standard Auxiliary
-
COM1: port
Note that the stdprn and stdaux streams are not always defined. This is because LPT1: and COM1: have no meaning under certain operating systems. However, stdin, stdout, and stderr are always defined. Also, note that the stdin stream does not have to come from the keyboard; it can come from a disk file or some other device through what is called redirection. In the same manner, the stdout stream does not have to appear on-screen; it too can be redirected to a disk file or some other device. See the next FAQ for an explanation of redirection.
No comments:
Post a Comment