#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2003
    Location
    Australia
    Posts
    13
    Rep Power
    0

    DOS parameter passing


    Hi everyone,

    I have a quick question, which should be relatively simple.......

    I have built a program in VC++ V6. When I run the program from a DOS prompt, I need to be able to call it like:
    <programname> 100 test.txt
    where program name is the compiled exe file (main.exe in my case I think).
    eg
    main 100 test.txt

    I have set up the main function as;
    Code:
    void main(int argc, char* argv[]) {
    }
    I can return the parameters in argv[], but the number is stored as a string. I need some way to either read this parameter as an integer, or be able to convert from char to int.
    And, at the same time, I still need the filename to be a string (so I can specify that from the command prompt too).

    Would anyone know of how this can be done?

    Many thanks......
    -Andrew
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,169
    Rep Power
    2222
    As the programmer, you have the power and the authority to define the order of the parameters -- unless you are working from a spec. But even then, the first parameter should be defined to mean one thing, the second another, etc. And you know what those definitions are, so you know which ones to convert to integer and which ones to use as strings, etc.

    To convert a string to an integer value, use atoi(). To convert a float, use atof. Or you could use sscanf if you're more comfortable with that.

    Code:
    int int_value;
    
    int_value = atoi(argv[1]);
    BTW, I trust that you are aware that the first parameter, argv[0], is the name of the program.
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2003
    Location
    Australia
    Posts
    13
    Rep Power
    0
    Hi Dwise1.......

    Unfortunately, I am working from a spec...... :(

    But thanks for the reply. I think its excactly what I need.

    Cheers,
    -Andrew
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2003
    Posts
    52
    Rep Power
    12

    you should use int main() because its more proper
    Roar!!!
  8. #5
  9. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2003
    Location
    Australia
    Posts
    13
    Rep Power
    0
    G'day Seb,

    Yeah, thats true. But, thruth be told, I actually "acquired" the code from somewhere else, then expanded it to my own functionality, and Ive left it "as is" in that respect.

    Yes, the source of the code has been correctly referenced......

    BTW - Love the link.........

    -Andrew

IMN logo majestic logo threadwatch logo seochat tools logo