The Standard Streams. McGraw-Hill The Complete Reference Fourth Edition
Publicitate
Publicitate
232
C + + : T h e C o m p l e t e R e f e r e n c e
A word of warning: Although fprintf( ) and fscanf( ) often are the easiest way to write and read assorted data to disk files, they are not always the most efficient.
Because formatted ASCII data is being written as it would appear on the screen
(instead of in binary), extra overhead is incurred with each call. So, if speed or file size is a concern, you should probably use fread( ) and fwrite( ).
The Standard Streams
As it relates to the C file system, when a program starts execution, three streams are opened automatically. They are stdin (standard input), stdout (standard output), and
stderr
(standard error). Normally, these streams refer to the console, but they may be redirected by the operating system to some other device in environments that support redirectable I/O. (Redirectable I/O is supported by Windows, DOS, Unix, and OS/2, for example.)
Because the standard streams are file pointers, they may be used by the C I/O system to perform I/O operations on the console. For example, putchar( ) could be defined like this: int putchar(char c)
{ return putc(c, stdout);
}
In general, stdin is used to read from the console, and stdout and stderr are used to write to the console.
You may use stdin, stdout, and stderr as file pointers in any function that uses a variable of type FILE *. For example, you could use fgets( ) to input a string from the console using a call like this: char str[255]; fgets(str, 80, stdin);
In fact, using fgets( ) in this manner can be quite useful. As mentioned earlier in this book, when using gets( ) it is possible to overrun the array that is being used to receive the characters entered by the user because gets( ) provides no bounds checking. When used with stdin, the fgets( ) function offers a useful alternative because it can limit the number of characters read and thus prevent array overruns. The only trouble is that
fgets( )
does not remove the newline character and gets( ) does, so you will have to manually remove it, as shown in the following program.
C h a p t e r 9 : F i l e I / O
233
#include <stdio.h>
#include <string.h> int main(void)
{ char str[80]; int i; printf("Enter a string: "); fgets(str, 10, stdin);
/* remove newline, if present */ i = strlen(str)-1; if(str[i]=='\n') str[i] = '\0'; printf("This is your string: %s", str);
} return 0;
Keep in mind that stdin, stdout, and stderr are not variables in the normal sense and may not be assigned a value using fopen( ). Also, just as these file pointers are created automatically at the start of your program, they are closed automatically at the end; you should not try to close them.
The Console I/O Connection
There is actually little distinction between console I/O and file I/O. The console I/O functions described in Chapter 8 actually direct their I/O operations to either stdin or
stdout
. In essence, the console I/O functions are simply special versions of their parallel file functions. The reason they exist is as a convenience to you, the programmer.
As described in the previous section, you can perform console I/O using any of the file system functions. However, what might surprise you is that you can perform disk file I/O using console I/O functions, such as printf( )! This is because all of the console
I/O functions operate on stdin and stdout. In environments that allow redirection of I/O, this means that stdin and stdout could refer to a device other than the keyboard and screen. For example, consider this program:
#include <stdio.h>
234
C + + : T h e C o m p l e t e R e f e r e n c e int main(void)
{ char str[80]; printf("Enter a string: "); gets(str); printf(str);
} return 0;
Assume that this program is called TEST. If you execute TEST normally, it displays its prompt on the screen, reads a string from the keyboard, and displays that string on the display. However, in an environment that supports I/O redirection, either stdin, stdout, or both could be redirected to a file. For example, in a DOS or Windows environment, executing TEST like this:
TEST > OUTPUT causes the output of TEST to be written to a file called OUTPUT. Executing TEST like this:
TEST < INPUT > OUTPUT directs stdin to the file called INPUT and sends output to the file called OUTPUT.
When a program terminates, any redirected streams are reset to their default status.
Using freopen( ) to Redirect the Standard Streams
You can redirect the standard streams by using the freopen( ) function. This function associates an existing stream with a new file. Thus, you can use it to associate a standard stream with a new file. Its prototype is
FILE *freopen(const char *filename, const char *mode, FILE *stream); where filename is a pointer to the filename you wish associated with the stream pointed to by stream. The file is opened using the value of mode, which may have the same values as those used with fopen( ). freopen( ) returns stream if successful or NULL on failure.
C h a p t e r 9 : F i l e I / O
235
The following program uses freopen( ) to redirect stdout to a file called OUTPUT:
#include <stdio.h> int main(void)
{ char str[80]; freopen("OUTPUT", "w", stdout); printf("Enter a string: "); gets(str); printf(str);
} return 0;
In general, redirecting the standard streams by using freopen( ) is useful in special situations, such as debugging. However, performing disk I/O using redirected stdin and stdout is not as efficient as using functions like fread( ) or fwrite( ).
This page intentionally left blank
Chapter 10
The Preprocessor and Comments
237
Copyright © 2003 by The McGraw-Hill Companies. Click here for terms of use.
Descarcă
Publicitate