Thread: Command line args [was http://faq.cprogr....]

  1. #1
    Registered User
    Join Date
    Dec 2003
    Posts
    41

    http://faq.cprogramming.com/cgi-bin/smartfaq.cgi?answer=1048384537&id=1043284392

    Hi - I'm doing the same uni project as warny_maelstrom. Interesting thread that... anywho - I've been going through the faq's trying to get my head around this - seeing as a chunk of code was dumped in warny's thread im a little behind, i couldn't start from the beginning. I'm looking at the faq

    http://faq.cprogramming.com/cgi-bin/...&id=1043284392

    and in the first example there are arguments passed into the main function...

    Code:
    int main ( int argc, char *argv[] )
    {
      int ch;
      FILE *in;
      FILE *out;
    
      if ( argc != 3 ) {
        fprintf ( stderr, "Usage: %s <readfile1> <writefile2>\n", argv[0] );
        exit ( EXIT_FAILURE );
      }
    ...
    ...
    ...
    As most will know, i'm a little new and a little inquisitive. So if anyone can answer my question - are you allowed to do this and if so, what purpose would you do so under?

    Cheers, and expect more soon

    Mitch
    ~~~~~~~~~~
    Mitchell Kent
    07782383326
    [email protected]
    ~~~~~~~~~~

  2. #2

    Re: http://faq.cprogramming.com/cgi-bin/smartfaq.cgi?answer=1048384537&id=1043284392

    Originally posted by spudtheimpaler
    ...
    and in the first example there are arguments passed into the main function...
    ...So if anyone can answer my question - are you allowed to do this and if so, what purpose would you do so under?

    Cheers, and expect more soon

    Mitch
    if I read the question correctly, you're asking why arguments are passed into the main? yes?

    if so - passing arguments into main if perfectly legal and necessary when the program needs to read files off of the command line. say you have a program will take in two files and create a third that is the combination of both files.

    >prog file1 file2 file3

    well if you don't pass in what was read from the command line (i.e. argc and argv) then the program will not be able to use the information on the command line.

    argc represents the number of arguments on the command line (in case above - 4) and argv is the array that contains the command line information, including the name of the program.

    the example that was in your post was just error trapping the information passed, if there was't 3 command line arguments, then the program ended.
    DrakkenKorin

    Get off my Intarweb!!!!

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    In your point-and-click world, arguments to main are seldom used. Unless you're modifying your "shortcut" (for all you Windows users) to have command line parameters, you usally never ever have arguments to main.

    However, in DOS / *nix / many other OSs, it's quite common to have more than a single word on a command line. For example, take the man command. Usage would be something like:

    man fgets

    Which gives you a nice little listing of what fgets does. (See the link for the output you'd get from said command.)

    In fact, in most command line based systems, you'd probably be passing arguments on the command line at least half of the time. If not more.

    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Try the following:

    Code:
    #include <stdio.h>
    
    int main(int argc, char *argv[])
    {
      int i;
    
      printf("\\Number of command line arguments = %d\n\n", argc);
      for (i = 0; i < argc; i++) {
        printf("Arg %d: <%s>\n", i, argv[i]);
      }
      printf("Press 'Enter' to exit the program (anyone see the irony here?) ");
      getchar(); /* this is necessary if you invoke by clicking; otherwise
                  * the window will not remain open after the information
                  * is printed out */
      return 0;
    }
    Invoke this from a "DOS" window, and try different command line arguments.

    What happens in each case:

    printargs

    printargs 1 2 3

    printargs "This is a string" /option 1

    (Now you try some with your arguments.)

    Have fun!

    Dave

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 10-24-2005, 01:32 PM
  2. Replies: 1
    Last Post: 06-29-2004, 05:23 PM
  3. Multiple Command Line Args
    By mart_man00 in forum C Programming
    Replies: 10
    Last Post: 08-16-2002, 02:15 PM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM