About arguments

How can I use arguments that my program takes when it starts, for example void main (int argc, char *argv[]).

I mean how do I compare or read them to arrays, for example? I have Borland C++ 4.52 and Intel Pentium 350 Hz (65 mb RAM, 6,2 Gb RAM).




Comments

  • : How can I use arguments that my program takes when it starts, for example void main (int argc, char *argv[]).

    : I mean how do I compare or read them to arrays, for example? I have Borland C++ 4.52 and Intel Pentium 350 Hz (65 mb RAM, 6,2 Gb RAM).


    The first number is the number of space separated strings given on the commandline.

    The pointer is an array of strings.


    Say your program is foo, and you run it:

    foo input.txt output.txt


    I believe argc will be 2, and the array will be

    argv[0] is input.txt

    argv[1] is output.txt


    I think that's how it works. If you make a test program, and inspect argv, that might help you see if I'm just spouting gibberish.


    MLINK







  • : How can I use arguments that my program takes when it starts, for example void main (int argc, char *argv[]).


    Okay...


    argc indicates the number of parameters that are in argv (since argv is an undimensioned array of character pointers, you need to know indirectly how big the array is -- the array can't tell you itself).


    Each element in argv is a character pointer that just happens to point to a string. The operating system that calls you will set up argc and argv for you. So argv is just a table of C style null-terminated strings.


    So you can use them as the source for any string routines (I highly recommend you DON'T write to any element of argv or change any of the underlying strings, though).


    argc will always be at least 1 (the name of the program that the operating system thinks is executing). That means that argv[0] will always point to an area of memory where the name of the program exists.


    If argc is greater than 1 then more parameters exist in argv. The valid indices of argv are, obviously, from 0 (argv[0] is the name of the program) to argc-1. They are each strings


    You can output any of these like any C string. You can strcpy from it. You can strcmp with it. You can index into it (argv[0][0] is the character in the first array position of the first string).


    argv[1] will be the first parameter AFTER the program name, argv[2] will be the second, and so on. They are always in C string format.


    Even numbers will be in ASCII string format which means you must convert the string to a number (sscanf can do this. atoi and atod can convert integers and doubles. an istrstream object can stream out anything streamable).


    You can do anything with this array-of-strings (or table-of-strings, as I called it before) you want, and anything you want to the strings that each array element points to, but I recommend, as I said, against changing either the array entries, or the string data.





Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories