- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
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
Link Copied
13 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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)...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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 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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Or wait a week...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Will Intel be giving us a nice surprise in accessing the command line from IVF?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
If I told you, it wouldn't be a surprise, now, would it?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Too true.
But I was always that kid who was searching for the presents well before Christmas.
Ed
But I was always that kid who was searching for the presents well before Christmas.
Ed
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
It's fine to use f2kcli for now.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
That's fine too - just keep in mind that these routines vary in interface across implementations.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
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.

Reply
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page