LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   How can I compile program to use command line arguments (https://www.linuxquestions.org/questions/programming-9/how-can-i-compile-program-to-use-command-line-arguments-673731/)

njac 10-02-2008 08:19 AM

How can I compile program to use command line arguments
 
How can I compile program to use command line arguments?
for example:

./a.out -filename

Compiler is g77.
thanks!

jf.argentino 10-02-2008 09:17 AM

I don't know how it works in fortran but, as it's service provided by the OS, similar mechanism must exist. In C, the entry point is always the function called "main", and the system give to this function two parameters: the first one is an integer and the second one is an array of strings, the number of strings in the array is given by the first parameter. The function prototype is:
Code:

int main (int argc, char* argv[]);
The first string of the second parameter is the program name typed in the command line. Then if you type "./a.out -t foo -v bar", then the system will pass to your main function:
argc = 5
argv[0] = "./a.out"
argv[1] = "-t"
argv[2] = "foo"
argv[3] = "-v"
argv[4] = "bar"
The standard C library provide an helper to ease the arguments handling, its name is "getopt", "man 3 getopt" will give you all the details you need.

njac 10-02-2008 09:34 AM

Thank you for your answer!
Well, now I have basic idea how it works, but fortran is quite different story. Can someone tell me more??

paulsm4 10-02-2008 10:36 AM

Hi -

http://gcc.gnu.org/onlinedocs/gcc-3.3.6/g77/

Look for "GetArg" and "IArgC" intrinsics.

njac 10-03-2008 03:50 AM

This is realy simple, just call:
call getarg(n, arg)
,where:

integer n -is number of argument
character (len= ..) arg -is n-th argument

For example, this:

character (len=10) arg
call getarg(2, arg)
print*, arg
end


with this:

./a.out aaa bbb

gives output:

bbb

Thank you for your help.


All times are GMT -5. The time now is 09:54 PM.