Problem with command line arguments
Hi all,
I was reading Lesson 14: Accepting command line argements. There was a sample program. I manage to compile it but when executed, this message appeared:
usage: D:\CommandArg\Debug\CommandArg.exe <filename>
Press any key to continue
The thing is, i wasn't even able to enter any file name. Upon clicking the execute program button, this message appears right away. Can someone help me with this? I am using Visual C++ 6. Thank you.
The code from lesson 14 is as follows:
Code:
#include <fstream>
#include <iostream>
using namespace std;
int main ( int argc, char *argv[] )
{
if ( argc != 2 ) // argc should be 2 for correct execution
// We print argv[0] assuming it is the program name
cout<<"usage: "<< argv[0] <<" <filename>\n";
else {
// We assume argv[1] is a filename to open
ifstream the_file ( argv[1] );
// Always check to see if file opening succeeded
if ( !the_file.is_open() )
cout<<"Could not open file\n";
else {
char x;
// the_file.get ( x ) returns false if the end of the file
// is reached or an error occurs
while ( the_file.get ( x ) )
cout<< x;
}
// the_file is closed implicitly here
}
}