Advertisement

OT: passing variables from line command

Started by December 16, 2001 05:45 AM
6 comments, last by penetrator 23 years, 2 months ago
when i run a program like "test.exe -video:1" how do i catch the -video parameter in my c++ code ? glHorizon_Project

www.web-discovery.net

Simple:

    int main( int argc, char **argv ){    for( int i = 1; i < argc; i++ )    {        // argv points to the ith parameter on the command-line.<br></font><br>    }<br>}<br>  </pre></font></td></tr></table></center><!–ENDSCRIPT–>     <br><br>In your case, argv[0] would be "test.exe" and argv[1] would be "-video:1".<br><br><hr size=1><a href="http://www.codeka.com/">codeka.com</a> - Just click it.<br><br>Edited by - Dean Harding on December 16, 2001 6:58:36 AM    
Advertisement
Of course, in the example above the loop would never execute because of the simple fact that i starts out equal to argc (1) in the case outlined by penetrator.

argc is the number of command-line arguments, including the full name and path of the executable (ie, argv[0] is always the name of the application). argv[] is an array of null-terminated string (C-style) which are the various arguments passed. Since argv[] is an array of strings, you''ll need to take care to convert numeric parameters properly.

[ GDNet Start Here | GDNet FAQ | MS RTFM | STL | Google ]
Thanks to Kylotan for the idea!
you say:
1) argc is the number of command-line arguments, including the full name and path of the executable

and:
2) i starts out equal to argc (1) in the case outlined by penetrator.

this is a contradiction, according to 1) in penetrators case argc would be 2, but you say in 2) that it is 1!
in WinMain() use LPSTR lpCmdLine. It is a string with all the parameters w/o the "test.exe".
in WinMain() use LPSTR lpCmdLine. It is a string with all the parameters w/o the "test.exe".

------------------------------
Baldur K

"It will be happened; it shall be going to be happening; it will be was an event that could will have been taken place in the future."
--time travel explained by Rimmer (Red Dwarf)

"If you don''t gosub a program loop, you''ll never get a subroutine."
-- Kryten (Red Dwarf)
Advertisement
> Of course, in the example above the loop would never execute because of the simple fact that i starts out equal to argc (1) in the case outlined by penetrator.

Yes it would In Penetrators example argc is 2. The loop will execute once.
quote:
Original post by Oluseyi
Of course, in the example above the loop would never execute because of the simple fact that i starts out equal to argc (1) in the case outlined by penetrator.



The example is correct; argc is 2 because it includes the name of the executable. It starts at 1 so that it doesn''t count the executable name as the first argument. That''s usually what you want...


codeka.com - Just click it.

This topic is closed to new replies.

Advertisement