Posted: Sun Nov 29, 2009 10:21 pm Post subject: Command Line Arguments
I'm using a tutorial and I got to command line arguments. I don't get some of the code:
#include <fstream>
#include <iostream>
using namespace std;
int main ( int argc, char *argv[] )
{
if ( argc != 2 ) // argc should be 2 for correct execution
cout<<"usage: "<< argv[0] <<" <filename>\n";
else {
ifstream the_file ( argv[1] );
if ( !the_file.is_open() )
cout<<"Could not open file\n";
else {
char x;
while ( the_file.get ( x ) )
cout<< x;
}
}
}
Why does argc have to be 2 for it to be correct?
What is this program trying to do?
Sponsor Sponsor
Tony
Posted: Sun Nov 29, 2009 10:32 pm Post subject: RE:Command Line Arguments
The correct usage of this program requires the argument counter to be 2: program name + filename. Try running the program to see what it does (or read the source code).