Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

passing arguments to a program

ekeom
Novice
1,388 Views
Hello,

I wrote a f90 program. I would like to execute it using the dos commands window. I need to pass some arguments : string and real.

I had try the getarg function. It works well, but only for string. As show an this exemple.

program test
use dflib
integer(2) n1, n2, status
character(80) buf
n1 = 1
n2 = 2
call getarg(n1, buf, status)
write(*,*) buf
call getarg(n2, buf, status)
write (*,*) buf
end program test

How Can I pass real and string arguments to a program?

Best regards,

Didace
0 Kudos
13 Replies
h-faber
Beginner
1,388 Views
Hi,
afaik it is only possible to submit Strings, or at least arguments are recognized only as Strings. So the only chance seems to be parsing the string and converting to float or whatsoever. But don't ask me how to do this in Fortran, in Java it is simple: Float.parseFloat(yourString)...
0 Kudos
anthonyrichards
New Contributor III
1,388 Views
If you want to send some numbers on the command line, just enclose them within quotes e.g.
myprogram. exe filename '0.3 0.2 34 3.7d-01 101'
Using your code, they will turn up in 'buf' and you can read them using an internal read such as
READ(BUF,*, err=999, end=888) REAL1, REAL2, INTEGER1, REAL3, INTEGER2,.....
and so on.
0 Kudos
emc-nyc
Beginner
1,387 Views
To convert from a string to a real, integer, or logical, use an internal read.

I tend to do it something like this:

integer :: int
real :: value

character(len=100) :: string

character(len=12) :: fmt

string = "1.234"

write(fmt,'("(G",i0,".0)")') len_trim(string)

read(string,fmt) value

Note that "G" formatting is smart enough to deal with reals, in either conventional (1.234) or exponential (1.23e+10) form or integers. I think it works for logicals, too, but I haven't tried reading a logical since college.

I tend not to use

read(string, *) value

because I've seen it choke when the string is less than 12 characters long.
0 Kudos
mr_peanut
Beginner
1,388 Views
I haven't tried it myself but you might want to give this a try:
It isa free implementation of the proposed Fortran F2003 command line interface for many different Fortran compilers.
0 Kudos
Steven_L_Intel1
Employee
1,388 Views
Or wait a week...
0 Kudos
emc-nyc
Beginner
1,387 Views
Will Intel be giving us a nice surprise in accessing the command line from IVF?
0 Kudos
Steven_L_Intel1
Employee
1,387 Views
If I told you, it wouldn't be a surprise, now, would it?
0 Kudos
emc-nyc
Beginner
1,388 Views
Too true.

But I was always that kid who was searching for the presents well before Christmas.

Ed
0 Kudos
TimP
Honored Contributor III
1,388 Views
If you can't wait, f2kcli web site is still up. http://www.winteracter.com/f2kcli/
0 Kudos
Steven_L_Intel1
Employee
1,388 Views
It's fine to use f2kcli for now.
0 Kudos
emc-nyc
Beginner
1,387 Views
I'm muddling through with nargs/getarg quite nicely....I kind of prefer these names to the rather baroque ones which seem to be coming out of j3.
0 Kudos
Steven_L_Intel1
Employee
1,388 Views
That's fine too - just keep in mind that these routines vary in interface across implementations.
0 Kudos
emc-nyc
Beginner
1,388 Views
It's easy in Fortran, too:

read(string,format) value

for integers, setting format = '(i0)' will almost always work.

read(string, *) value

will probably work, too, although I've seen it fail when len(string) is too short -- 'too short' depending on the implementation of the compiler. The asterisk is a 'default' format, which will read string based on the type of value.
0 Kudos
Reply