Using command-line arguments - C++ Function

C++ examples for Function:Math Function

Description

Using command-line arguments

Demo Code

#include <iostream> 
#include <fstream> 
using namespace std; 

int main( int argc, char *argv[] )
{ 
   if ( argc != 3 ) 
       cout << "Usage: copyFile infile_name outfile_name" << endl; 
   else { /*from   w  w  w .  j a va2s  .co m*/
       ifstream inFile( argv[ 1 ], ios::in ); 

       // input file could not be opened 
       if ( !inFile ) 
       { 
           cout << argv[ 1 ] << " could not be opened" << endl; 
           return -1; 
       } 

       ofstream outFile( argv[ 2 ], ios::out ); 

       // output file could not be opened 
       if ( !outFile ) 
       { 
           cout << argv[ 2 ] << " could not be opened" << endl; 
           inFile.close(); 
           return -2; 
       }

       char c = inFile.get(); // read first character 

       while ( inFile ) 
       { 
           outFile.put( c );       // output character 
           c = inFile.get();       // read next character 
       } 
    }  // end else 
}

Related Tutorials