-
command line arguments
this is merely out of curiosity,
i have read a couple of articles recently on the arguments that get parsed/passed(?) by int main , and some of the situations where these arguments are filtered/checked and a relevant process is invoked....
what i haven't been able to read anywhere, is how a app can send arguments to int main when when the application starts up or during application runtime, or if this is possible, or how these arguments arise in argc argv. from what i read, a file could be one of the arguments, how does this file land up there, say for instance i open and read a file in a program, does this automatically become one of the arguments, or is a file specifically run from a command line argument......
i dont really understand this, but i hope my questions are clear enough to get the correct answers.
-
The easiest way to pass arguments to a program is through the command line. Everything after the application's file name is passed as an argument to the application. For example, will invoke the "foo" program and pass it the arguments "bar1" and "bar2". These are in addition to "foo", which is how we invoked the program and is always the first argument supplied.
Now you might be thinking that now all applications are started from the command line. To invoke an application from within another application and pass it arguments, one can use a member of the exec() family of functions.
http://www.opengroup.org/onlinepubs/...ions/exec.html
Hope that helps
-
Command line arguments (or parameters) are simply strings. Whether these strings represent filenames or switches or whatever is up to you.
From main()'s point of view, command line arguments are just arguments to the function main() itself, in the form of argc and argv. In other words, all of the arguments are supplied when the program is first started. You can't pass in parameters to a program at any time, only when the program is initially executed and main() is initially called.
From a shell's point of view, you type strings to be executed. For example, I might type "test" into a shell (perhaps bash for Linux, or the standard Command Prompt for Windows). The shell should then execute the program "test". If I typed "test message", the program "test" would be executed, with one parameter: "message". "test this message" would result in two parameters, and so on.
There's also an FAQ about this, though it's not very extensive. http://faq.cprogramming.com/cgi-bin/...&id=1043284392
-
thanks for the explanations.