Thread: Problem with command line arguments

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    4

    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
      }
    }

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Are you running this from within the IDE? If so you need to set the arguments that will be passed into your program under Project->Settings->Debug Tab->Program Arguments. Type your argument into the text box and click on OK and then execute your program.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  2. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  3. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  4. C++ arguments problem
    By swanley007 in forum C++ Programming
    Replies: 9
    Last Post: 08-01-2006, 12:32 PM
  5. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM