home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / dos / diverses / tctnt / cincoutd.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1991-08-27  |  4.4 KB  |  168 lines

  1. /* CINCOUTD.CPP: Redirecting CIN or COUT.
  2.  
  3.          In the following program, the input with CIN can be redirected
  4.          to gather the data from a file, or the output with COUT can be
  5.          redirected to a file or stdprn depending on the program's
  6.          command line arguments.
  7. */
  8.  
  9. #include <fstream.h>
  10. #include <iostream.h>
  11. #include <io.h>
  12. #include <fcntl.h>
  13. #include <process.h>
  14. #include <string.h>
  15. #include <sys\stat.h>
  16.  
  17. #define  PRNHANDLE  4
  18. #define  ARRAYSIZE  80
  19.  
  20. void OutputUsage( void );
  21. int  OpenOutFile( char * );
  22. int  OpenInFile( char * );
  23.  
  24. int main( int argc, char *argv[] )
  25. {
  26.         char      io = 0;             //Keeps track if CIN or COUT redirection
  27.         filebuf   *fBuf;              //Filebuf created with stdprn filehandle
  28.         streambuf *oldBuf;            //Used to hold the old streambuf while CIN
  29.                                                                     //  or COUT is being redirected
  30.         unsigned  fhndl = -1;         //File handle of redirected file
  31.         char      a[ARRAYSIZE + 1];   //Used for the sample I/O
  32.  
  33.         if ( argc != 2 ) {
  34.             switch( argv[1][0] ) {      //Check the second command argument
  35.  
  36.             // Redirect CIN to gather from file specified at
  37.             // command line
  38.                 case 'I':
  39.                 case 'i':
  40.  
  41.                     // Capture CIN's current streambuf before it is reassigned
  42.                     oldBuf = cin.rdbuf();
  43.                     io = 'i';
  44.  
  45.                     // Opens file specified at command line and redirects
  46.                     // CIN to extract from it
  47.                     fhndl = OpenInFile( argv[2] );
  48.                     break;
  49.  
  50.                 // Redirect COUT to output to file specified
  51.                 // at the command line
  52.                 case 'O':
  53.                 case 'o':
  54.  
  55.                     // Capture COUT's streambuf before new
  56.                     // streambuf * is assigned.
  57.                     oldBuf = cout.rdbuf();
  58.                     io = 'o';
  59.  
  60.                     // Opens file specified at command line and
  61.                     // redirects output via COUT to file
  62.                     fhndl = OpenOutFile( argv[2] );
  63.                     break;
  64.  
  65.                 // Redirect COUT to stdprn
  66.                 case 'P':
  67.                 case 'p':
  68.  
  69.                     // Create a new filebuf with the file handle to stdprn
  70.                     fBuf = new filebuf( PRNHANDLE );
  71.  
  72.                     // Use overloaded assignment operator to assign
  73.                     // new filebuf to COUT
  74.                     cout = fBuf;
  75.                     break;
  76.                 default:
  77.  
  78.                     // If no command line arguments specified, use
  79.                     // default settings for CIN and COUT
  80.                     if ( argc == 1 ) {
  81.                         break;
  82.                     }
  83.                     // Output command line usage if 2nd command
  84.                     // line argument incorrect
  85.                     OutputUsage();
  86.                 }
  87.         }
  88.  
  89.         // NOW NORMAL USE OF CIN AND COUT WILL HONOR ANY
  90.         // REDIRECTION DONE ABOVE
  91.  
  92.         // Simple demonstration of CIN inserting characters
  93.         // into array 'a'
  94.         cin.getline( a, ARRAYSIZE );
  95.  
  96.         // Check to make sure the previous function call did
  97.         // not produce errors
  98.         if( cin.fail() ) {
  99.             return 1;
  100.         }
  101.  
  102.         // Simple demonstration of COUT outputting contents
  103.         // of 'a' to output stream
  104.         cout << a << endl;
  105.  
  106.         // If a file was opened for redirection, close that file
  107.         if ( fhndl != -1 ) {
  108.                 close( fhndl );
  109.  
  110.             // Reassign the old streambuf * to either CIN or COUT
  111.             if ( io == 'i' ) {
  112.                 cin = oldBuf;
  113.             }
  114.             else
  115.                 if ( io == 'o' ) {
  116.                     cout = oldBuf;
  117.                 }
  118.             // NOW CIN AND COUT HAVE BEEN SET TO ORIGINAL STATE
  119.         }
  120.         return 0;
  121. }
  122.  
  123. int OpenOutFile( char *filename ) {
  124.     filebuf *tmpFBuf;
  125.     int fhndl;
  126.     fhndl = open( filename, O_CREAT|O_TEXT|O_RDWR, S_IREAD|S_IWRITE );
  127.     if ( fhndl == -1 ) {
  128.         cout << "Error opening file " << filename << ", program terminated.";
  129.         cout << endl;
  130.         exit(1);
  131.     }
  132.  
  133.     // Create new filebuf with file handle of file specified
  134.     // at command line
  135.     tmpFBuf = new filebuf( fhndl );
  136.  
  137.     // Use overloaded assignment operator to assign new
  138.     // filebuf to COUT
  139.     cout = tmpFBuf;
  140.     return fhndl;
  141. }
  142.  
  143. int OpenInFile( char *filename ) {
  144.     filebuf *tmpFBuf;
  145.     int fhndl;
  146.     fhndl = open( filename, O_CREAT|O_TEXT|O_RDONLY, S_IREAD|S_IWRITE );
  147.     if ( fhndl == -1 ) {
  148.         cout << "Error opening file " << filename << ", program terminated.";
  149.         cout << endl;
  150.         exit(1);
  151.     }
  152.  
  153.     // Create new filebuf with file handle of file specified at command line
  154.     tmpFBuf = new filebuf( fhndl );
  155.  
  156.     // Use overloaded assignment operator to assign new filebuf to CIN
  157.     cin = tmpFBuf;
  158.     return fhndl;
  159. }
  160.  
  161. void OutputUsage( void ) {
  162.     cout << endl << "Usage: <exe filename> [I, O, ?, h] <filename>" << endl;
  163.     cout << "I - cin will be redirected to input from  <filename>." << endl;
  164.     cout << "O - cout will be redirected to output to <filename>." << endl;
  165.     cout << "P - cout will be redirected to output to stdprn." << endl;
  166.     cout << "? or h - Outputs this screen." << endl;
  167. }
  168.