Using command-line arguments for copying files : command line arguments « Development « C++ Tutorial






#include <iostream>
using std::cout;
using std::endl;
using std::ios;

#include <fstream>
using std::ifstream;
using std::ofstream;

int main( int argc, char *argv[] )
{
   // check number of command-line arguments
   if ( argc != 3 )
      cout << "Usage: copyFile infile_name outfile_name" << endl;
   else
   {
      ifstream inFile( argv[ 1 ], ios::in );
      if ( !inFile )
      {
         cout << argv[ 1 ] << " could not be opened" << endl;
         return -1;
      }

      ofstream outFile( argv[ 2 ], ios::out );
      if ( !outFile )
      {
         cout << argv[ 2 ] << " could not be opened" << endl;
         inFile.close();
         return -2;
      }

      char c = inFile.get();

      while ( inFile )
      {
         outFile.put( c );  
         c = inFile.get();
      }
   }
   return 0;
}
Usage: copyFile infile_name outfile_name








5.11.command line arguments
5.11.1.Display command-line arguments
5.11.2.Using command-line arguments for copying files
5.11.3.Sum the two numeric command line arguments