View Full Version : c++: command prompt execution with vars
So, suppose I have a program called "hello.exe" and I am using it from the command prompt. How do I pass in vars with the call to the program? (I really have no idea how to phrase it better, sorry :(.) For example:
hello.exe -g Jas
So the "-g" and "Jas" are passed into the program. Does that make even the slightest bit of sense?
tech_support
06-15-2008, 08:52 AM
Depends what language you've developed the program in.
You use the two parameters passed to the main function for that.
int main(int argc, char* argv[])
{
// argc is the total number of arguments (including the executable name)
// use argv to get the arguments
// argv[0] - the executable name ("hello.exe")
// argv[1] - "-j"
// argv[2] - "Jas"
}
C++ isn't as hard as I thought it would be to learn :)
Depends what language you've developed the program in.
Sorry. Its C++ (It's in the title, but I forgot it in the post.)
DimX: Thanks, that worked great after I played around with it. It's so obvious, and yet I never would have figured it out.
Is there a way to make it so that the teminal/command prompt will use characters like the pipe (|), amp (&), etc as regular characters in the call to my program?
For example:
hello.exe -g Jas "This|is|a|string"|with|"pipe|characters"|&|"amps"|and|quotes
magicyte
07-01-2008, 11:33 PM
As for the quotes, yes. Ampersand, yes. Pipe, so to speak, I don't know.
-magicyte
magicyte
07-07-2008, 06:02 PM
By the way, I forgot something to tell you. When taking in quotes("), the MS-DOS thing, WHATEVER, the stuff in between the two quotes counts as one whole character array. It order to take in multiple characters, you would need to create more variables in the main() function.
-magicyte
ps2death
03-01-2009, 02:51 AM
You use the two parameters passed to the main function for that.
int main(int argc, char* argv[])
{
// argc is the total number of arguments (including the executable name)
// use argv to get the arguments
// argv[0] - the executable name ("hello.exe")
// argv[1] - "-j"
// argv[2] - "Jas"
}
What do you mean up above next to the //. Do I have to include anything aswell, please show an example. Im just a noob, Im using Visual C++ 2008 Express Edition.
Two arguments are passed to main() by the operating system: an integer number of arguments passed, and a char*[] (array of strings [arrays of characters]) containing the arguments.
On POSIX systems there is a function getopt() to parse these options, but on Windows there's nothing.
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.