Passing Arguments to an Executable

Started by
16 comments, last by Geometrian 13 years, 7 months ago
Hi,

I'm not new to programming, but I am fairly new to C/C++.

I am trying to make an executable that can be called as so:
LinearResample.exe <input> <new width> <new height> <output>


I've never done anything of this sort before. I assume that this syntax would be handled through the argv statement?

So, for the above example, would argc be 4 and argv a pointer to an array, where array[0]==<input>, array[1]==<new width>, etc.?

Again, never done this before, so it would be great to know sorta what I'm doing.

Thanks,
G

[size="1"]And a Unix user said rm -rf *.* and all was null and void...|There's no place like 127.0.0.1|The Application "Programmer" has unexpectedly quit. An error of type A.M. has occurred.
[size="2"]

Advertisement
Very close [smile] The only thing missing is the fact that the first parameter is always the .exe file's name, so argc in your case would be 5, and argv[0] == "LinearResample.exe", and so on.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

Actually, the first parameter doesn't have to be the exe file name. On most platforms there are ways to invoke the program that will change argv[0] to arbitrary strings.
You can see for yourself rather simply, of course:

#include <cstdio>int main(int argc, char **argv){    for (int a = 0; a != argc; ++a)        printf("argv[%d] is \"%s\"\n", a, argv[a]);    return 0;}
Quote:Original post by SiCrane
Actually, the first parameter doesn't have to be the exe file name. On most platforms there are ways to invoke the program that will change argv[0] to arbitrary strings.


Really? How would it be done on, say Win32/Win64? (Just out of curiosity [smile])

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

Plain old CreateProcess:
Quote:
The flexibility of the CreateProcess() function (and a possible point of confusion) arises when you pass a valid string pointer to both the ApplicationName and CommandLine parameters. This allows you to specify the application to be executed as well as the complete command line that is passed to the application. One might assume that the command line passed to the created application is a composite of the ApplicationName and CommandLine parameters, but this is not the case. As a result, a process created by CreateProcess can receive a value other than its .exe name as its "argv[0]" parameter. The following is an example of a call to CreateProcess that produces this "abnormal" behavior:
Interesting... never noticed that before. Learn something new every day [smile]

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

Awesome.

So, what happens if I want to pass in a number, like 512, to the argument list?

This should be equivalent to:
char new_width = 512;

. . . which my compiler tells me results in a truncation/possible loss of data. Which makes sense, because 512 can't fit in a byte.

So . . . what do I do about this?

Thanks,
-G

[size="1"]And a Unix user said rm -rf *.* and all was null and void...|There's no place like 127.0.0.1|The Application "Programmer" has unexpectedly quit. An error of type A.M. has occurred.
[size="2"]

This is entirely from memory so may not compile or run [wink]

#include <sstream>#include <iostream>int main(int argc, char** argv){   int value = 0;   if(argc > 1)   {      std::stringstream converter;      converter << argv[1];      converter >> value;   }   std::cout << "Passed numeric value: " << value << std::endl;}

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

Well, the first and fourth arguments actually are chars and the second and third arguments actually are ints.

[size="1"]And a Unix user said rm -rf *.* and all was null and void...|There's no place like 127.0.0.1|The Application "Programmer" has unexpectedly quit. An error of type A.M. has occurred.
[size="2"]

This topic is closed to new replies.

Advertisement