Jump to content
  • Advertisement
Sign in to follow this  

Passing Arguments to an Executable

This topic is 2815 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


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

Share this post


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

Share this post


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

Share this post


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

Share this post


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

Share this post


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

Share this post


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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!