home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / c / other / file / typeit.c < prev   
Encoding:
C/C++ Source or Header  |  1988-10-31  |  1.7 KB  |  57 lines

  1. /* TYPEIT.C illustrates reassigning handles and streams using functions:
  2.  *      freopen         dup             dup2
  3.  *
  4.  * The example also illustrates:
  5.  *      setargv
  6.  *
  7.  * To make the program handle wild cards, link it with the SETARGV.OBJ
  8.  * file. You can do this in the QC environment by creating a program list
  9.  * containg TYPEIT.C and SETARGV.OBJ (include the path or put in current
  10.  * directory). You must also turn off the Extended Dictionary flag
  11.  * within the QC environment (Options-Make-Linker Flags) or use the /NOE
  12.  * linker option outside the environment. For example:
  13.  *    QCL typeit.c setargv /link /NOE
  14.  */
  15.  
  16. #include <stdio.h>
  17. #include <conio.h>
  18. #include <io.h>
  19. #include <process.h>
  20.  
  21. main( int argc, char **argv )
  22. {
  23.     FILE *ftmp;
  24.     int htmp;
  25.  
  26.     /* Duplicate handle of stdin. Save the original to restore later. */
  27.     htmp = dup( fileno( stdin ) );
  28.  
  29.     /* Process each command line argument. */
  30.     while( *(++argv) != NULL )
  31.     {
  32.         /* Original stdin used for getch. */
  33.         printf( "Press any key to display file: %s\n", *argv );
  34.         getch();
  35.  
  36.         /* Reassign stdin to input file. */
  37.         ftmp = freopen( *argv, "rb", stdin );
  38.  
  39.         if( (ftmp == NULL) || (htmp == -1 ) )
  40.         {
  41.             perror( "Can't reassign standard input" );
  42.             exit( 1 );
  43.         }
  44.  
  45.         /* Spawn MORE, which will receive the open input file as its standard
  46.          * input. MORE can be the DOS MORE or the sample program MORE.C.
  47.          */
  48.         spawnvp( P_WAIT, "MORE", NULL );
  49.  
  50.         /* Reassign stdin back to original so that we can use the
  51.          * original stdin to get a key.
  52.          */
  53.         dup2( htmp, fileno( stdin ) );
  54.     }
  55.     exit( 0 );
  56. }
  57.