Initializing Arrays

Chapter 11 - Complete I/O in C

Visual C++ 6: The Complete Reference
Chris H. Pappas and William H. Murray, III
  Copyright 1998 The McGraw-Hill Companies

Stream Functions
To use the stream functions, your application must include the file STDIO.H. This file contains definitions for constants, types, and structures used in the stream functions and contains function prototypes and macro definitions for the stream routines.
Many of the constants predefined in STDIO.H can be useful in your application. For example, EOF is defined to be the value returned by input functions at end-of-file, and NULL is the null pointer. Also, FILE defines the structure used to maintain information about a stream, and BUFSIZ defines the default size, in bytes, of the stream buffers.
Opening Streams
You can use one of three functions to open a stream before input and output can be performed on the stream: fopen( ), fdopen( ), or freopen( ). The file mode and form are set at the time the stream is opened. The stream file can be opened for reading, writing, or both, and can be opened in either text or binary mode.
All three functions return a file pointer, which is used to refer to the stream. For example, if your program contains the following line, you can use the file pointer variable pfinfile to refer to the stream:
pfinfile = fopen(“input.dat”,"r");
When your application begins execution, five streams are automatically opened. These streams are the standard input (stdin), standard output (stdout), standard error (stderr), standard printer (stdprn), and standard auxiliary (stdaux).
By default, the standard input, standard output, and standard error refer to the user’s console. This means that whenever a program expects input from the standard input, it receives that input from the console. Likewise, a program that writes to the standard output prints its data to the console. Any error messages that are generated by the library routines are sent to the standard error stream, meaning that error messages appear on the user’s console. The standard auxiliary and standard print streams usually refer to an auxiliary port and a printer, respectively.
You can use the five file pointers in any function that requires a stream pointer as an argument. Some functions, such as getchar( ) and putchar( ), are designed to use stdin or stdout automatically. Since the pointers stdin, stdout, stderr, stdprn, and stdaux are constants, not variables, do not try to reassign them to a new stream pointer value.
Input and Output Redirection
Modern operating systems consider the keyboard and video display as files. This is reasonable since the system can read from the keyboard just as it can read from a disk or tape file. Similarly, the system can write to the video display just as it can write to a disk or tape file.
Suppose your application reads from the keyboard and outputs to the video display. Now suppose you want the input to come from a file called SAMPLE.DAT. You can use the same application if you tell the system to replace input from the keyboard, considered now as a file, with input from another file, namely the file SAMPLE.DAT. The process of changing the standard input or standard output is called input redirection or output redirection.
Input and output redirection in MS-DOS are effortless. You use < to redirect the input and > to redirect the output. Suppose the executable version of your application is called REDIRECT. The following system-level command will run the program REDIRECT and use the file SAMPLE.DAT as input instead of the keyboard:
redirect < sample.dat
The next statement will redirect both the input (SAMPLE.DAT) and the output (SAMPLE.BAK):
redirect < sample.dat > sample.bak
This last example will redirect the output (SAMPLE.BAK) only:
redirect > sample.bak
  Note The standard error file stderr cannot be redirected.
There are two techniques for managing the association between a standard filename and a physical file or device: redirection and piping. Piping is the technique of directly connecting the standard output of one program to the standard input of another. The control and invocation of redirection and piping normally occur outside the program, which is exactly the intent since the program itself need not care where the data is really coming from or going to.
The way to connect the standard output from one program to the standard input of another program is to pipe them together by using the vertical bar symbol, |. Therefore, to connect the standard output of the program PROCESS1 to the standard input of the program PROCESS2, you would type
process1 | process2
The operating system handles all the details of physically getting the output from PROCESS1 to the input of PROCESS2.
Altering the Stream Buffer
All files opened using the stream functions (stdin( ), stdout( ), and stdprn( )) are buffered by default except for the preopened streams stderr and stdaux. The two streams stderr and stdaux are unbuffered by default unless they are used in either the printf( ) or scanf( ) family of functions. In this case, they are assigned a temporary buffer. You can buffer stderr and stdaux with setbuf( ) or setvbuf( ). The stdin, stdout, and stdprn streams are flushed automatically whenever they are full.
You can use the two functions setbuf( ) and setvbuf( ) to make a buffered stream unbuffered, or you can use them to associate a buffer with an unbuffered stream. Note that buffers allocated by the system are not accessible to the user, but buffers allocated with the functions setbuf( ) and setvbuf( ) are named by the user and can be manipulated as if they were variables. These user-defined stream buffers are very useful for checking input and output before any system-generated error conditions.
You can define a buffer to be of any size; if you use the function setbuf( ), the size is set by the constant BUFSIZ defined in STDIO.H. The syntax for setbuf( ) looks like this:
void setbuf(FILE *stream, char *buffer);
The following example program uses setbuf( ) and BUFSIZ to define and attach a buffer to stderr. A buffered stderr gives an application greater control over error exception handling. Using the integrated debugger, single-step the application exactly as you see it.
/*
*   setbf.c
*   A C program demonstrating how to define and attach
*   a buffer to the unbuffered stderr.
*   Copyright (c) Bill/Chris H. Pappas and William H. Murray, 1998
*/   

#include <stdio.h>
char cmyoutputbuffer[BUFSIZ];

void main(void)
{
  /
* associate a buffer with the unbuffered output stream */
  setbuf(stderr, cmyoutputbuffer); /
* line to comment out */

  /
* insert into the output stream buffer */
  fputs(“Sample output inserted into the\n”,stderr);
  fputs(“output stream buffer.\n”,stderr);

  /
* dump the output stream buffer */
  fflush(stderr);
}
Try running the program a second time with the setbuf( ) statement commented out. This will prevent the program from associating a buffer with stderr. When you ran the program, did you see the difference? Without a buffered stderr, the integrated debugger outputs each fputs( ) statement as soon as the line is executed.
The next application uses the function setvbuf( ). The syntax for setvbuf( ) looks like this:
int setvbuf(FILE *stream, char *buffer, int buftype, size_t bufsize);
Here, the program determines the size of the buffer instead of using BUFSIZ defined in STDIO.H:
/*
*   setvbuf.c
*   A C program demonstrating how to use setvbuf( )
*   Copyright (c) Bill/Chris H. Pappas and William H. Murray, 1998
*/

#include <stdio.h>
#define MYBUFSIZ 512

void main(void)
{
  char ichar, cmybuffer[MYBUFSIZ];
  FILE
*pfinfile, *pfoutfile;
  pfinfile = fopen(“sample.in”, “r”);
  pfoutfile = fopen(“sample.out”, “w”);

  if (setvbuf(pfinfile, cmybuffer, _IOFBF, MYBUFSIZ) != 0)
     printf(“pfinfile buffer allocation error\n”);
  else
     printf(“pfinfile buffer created\n”);

  if (setvbuf(pfoutfile, NULL, _IOLBF, 132) != 0)
     printf(“pfoutfile buffer allocation error\n”);
  else
     printf(“pfoutfile buffer created\n”);

  while(fscanf(pfinfile,"%c",&ichar) != EOF)
    fprintf(pfoutfile,"%c",ichar);

  fclose(pfinfile);
  fclose(pfoutfile);
}
The program creates a user-accessible buffer pointed to by pfinfile and a malloc( )-allocated buffer pointed to by pfoutfile. This last buffer is defined as buftype, _IOLBF, or line buffered. Other options defined in STDIO.H include _IOFBF, for fully buffered, and _IONBF, for no buffer.
Remember, both setbuf( ) and setvbuf( ) cause the user-defined buffer to be used for I/O buffering, instead of an automatically allocated buffer. With setbuf( ), if the buffer argument is set to null, I/O will be unbuffered. Otherwise, it will be fully buffered.
With setvbuf( ), if the buffer argument is null, a buffer will be allocated using malloc( ). The setvbuf( ) buffer will use the bufsize argument as the amount allocated and automatically free the memory on close.
Closing Streams
The two functions fclose( ) and fcloseall( ) close a stream or streams, respectively. The fclose( ) function closes a single file, while fcloseall( ) closes all open streams except stdin, stdout, stderr, stdprn, and stdaux. However, if your program does not explicitly close a stream, the stream is automatically closed when the application terminates. Since the number of streams that can be open at a given time is limited, it is a good practice to close a stream when you are finished with it.

Books24x7.com, Inc 2000 –  


Visual C++ 6(c) The Complete Reference
Visual Studio 6: The Complete Reference
ISBN: B00007FYGA
EAN: N/A
Year: 1998
Pages: 207

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net