Archived

This topic is now archived and is closed to further replies.

penetrator

OT: passing variables from line command

Recommended Posts

Simple:

    
int main( int argc, char **argv )
{
for( int i = 1; i < argc; i++ )
{
// argv[i] points to the ith parameter on the command-line.

}
}


In your case, argv[0] would be "test.exe" and argv[1] would be "-video:1".


codeka.com - Just click it.

Edited by - Dean Harding on December 16, 2001 6:58:36 AM

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
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)

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
> 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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites