Thread: Processing command line arguments

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    119

    Processing command line arguments

    I want my program to loop until there are no more command line arguments to process. Or do nothing if no arguments were entered. The command line arguments will be files that need to be read in. The user would be able to enter multiple files to be read in, or 1..or even none.

    Code:
    int main( int argc, char *argv[] )
    {
        //how can I tell if any arguments exist in the command line?
        
        for( i=0; i < number of command line arguments; i++ )//how can i tell how many there are to process?
        {
             input = fopen( argv[i], "r" ); //try reading the file passed as an argument in command line
        }
    }
    is it argc that contains the number of paramters entered in command line? When you type the program name, i.e program parameter1 paramter2...is the program name a parameter as well? would you start from argv[1] instead of argv[0] ?
    Last edited by John_L; 10-18-2007 at 02:04 PM.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by John_L View Post
    is it argc that contains the number of paramters entered in command line? When you type the program name, i.e program parameter1 paramter2...is the program name a parameter as well? would you start from argv[1] instead of argv[0] ?
    Yes, argc (or whatever name you call that parameter) contains the number of command-line arguments. This is always at least 1, the name (and path?) of the program that is running. argv[0] is the program name, if argc is at least 2, then there are other arguments available for you to examine/process starting at argv[1].
    "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

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    argc contains that number.

    Also, the FIRST argument, argv[0], is "the name of your program", so you need to start at 1, not zero for this particular type of loop. [It's the exception, really].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Sep 2007
    Posts
    119
    thanks. I was messing around and trying it out and when i was processing argv[0] i was getting wacky output so i assumed the program name was always the first parameter. I got it working great now. Thanks.

    p.s. is there a drawback to reading from command line, maybe I should give the user the option, make stdin available as well?

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    It is common that programs that take command line arguments also allow standard in as input, there are two forms:
    1. No argument means stdin is the input.
    2. An argument of "-" means stdin.

    There is no reason to not support BOTH of those if you like to do that - that way, it works like most people would expect in all circumstances.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    argv[0] must be a pointer to a valid string if argc is greater than zero, or NULL otherwise. What that string is doesn't matter, anything from an empty string to the executable's name is as likely and valid as far as standards are concerned. It's sort of important that the standards make that distinction as well, because there are more than enough ways to start an executable.

    There isn't really a drawback to using command line arguments, though I would expect most people would have the design sense to know what to read from stdin. Often if you're working with separate files, you have to use the command line to some extent so that you can easily interact with the user and work with the files at the same time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. GradeInfo
    By kirksson in forum C Programming
    Replies: 23
    Last Post: 07-16-2008, 03:27 PM
  2. command line arguments
    By vurentjie in forum C Programming
    Replies: 3
    Last Post: 06-22-2008, 06:46 AM
  3. NULL arguments in a shell program
    By gregulator in forum C Programming
    Replies: 4
    Last Post: 04-15-2004, 10:48 AM
  4. Using a lot of processing time!
    By nickname_changed in forum C++ Programming
    Replies: 0
    Last Post: 09-25-2003, 03:44 AM
  5. file writing crashes
    By test in forum C Programming
    Replies: 25
    Last Post: 08-13-2002, 08:44 AM