passing arguments to a program

passing arguments to a program


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,


14 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.

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)...

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.

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.

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.

Or wait a week...

Retired 12/31/2016

Will Intel be giving us a nice surprise in accessing the command line from IVF?

If I told you, it wouldn't be a surprise, now, would it?

Retired 12/31/2016

Too true.

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


If you can't wait, f2kcli web site is still up.

It's fine to use f2kcli for now.

Retired 12/31/2016

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.

That's fine too - just keep in mind that these routines vary in interface across implementations.

Retired 12/31/2016

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.

Leave a Comment

Please sign in to add a comment. Not a member? Join today