CFD Online Logo CFD Online URL
www.cfd-online.com
[Sponsors]
Home > Forums > General Forums > Main CFD Forum

Fortran direct input help

Register Blogs Community New Posts Updated Threads Search

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   March 14, 2007, 22:18
Default Fortran direct input help
  #1
Joe
Guest
 
Posts: n/a
Hi,

This is a fortran code problem but I figure out the many fortran programmers might be able to help.

Suppose I have a simple code like this:

program test

integer :: i

print *, "input i"

read *, i

print *, i

end program test

Running the code will prompt me to enter "i", which will be printed.

Is there a way whereby I can input directly?

ie ./a.out 10

and "10" will be printed.

I was told by my school's computer staff that this is only possible in C, not fortran.

Is that true? Thank you.
  Reply With Quote

Old   March 15, 2007, 01:26
Default Re: Fortran direct input help
  #2
Ananda Himansu
Guest
 
Posts: n/a
program test

integer :: i

read *, i

print *, i

end program test

Compile the above file, then run with

./a.out < 10

and "10" will be printed
  Reply With Quote

Old   March 15, 2007, 01:30
Default Re: Fortran direct input help
  #3
Markus Lummer
Guest
 
Posts: n/a
Hi Joe,

look into the Fortran manual for functions/subroutines for the processing of command line arguments, e.g. function iargc() and subroutine getarg(...).

Regards, Markus

  Reply With Quote

Old   March 15, 2007, 01:34
Default oops my bad
  #4
Ananda Himansu
Guest
 
Posts: n/a
on second thought, that will not work! Sorry, my mistake. you would need a file called "10" containing the input, in order for the code to work as I entered it. I guess the code the way you had it is the best that Fortran can do in general. There is no general mechanism in Fortran to parse command line arguments, though it is possible in Linux to enter

echo 10 | a.out

to achieve the effect you want
  Reply With Quote

Old   March 15, 2007, 04:20
Default Re: Fortran direct input help
  #5
Joe
Guest
 
Posts: n/a
Thanks Ananada & Markus.

I've tried the subroutine getarg. I am able to get a character sting but I need the arguments to be in real nos.

How can I do that? Is there something like a character to real no. conversion subroutine? I've searched google but nothing meaningful appear.

  Reply With Quote

Old   March 15, 2007, 04:31
Default Re: Fortran direct input help
  #6
Markus Lummer
Guest
 
Posts: n/a
Hi Joe,

in Fortran, you can read from a text string like from a file. In order to read the double precision variable number from the character string text, try e.g.

character text*(80) double precision number read(text,*) number

Regards, Markus
  Reply With Quote

Old   March 15, 2007, 04:32
Default Re: Fortran direct input help
  #7
Joe
Guest
 
Posts: n/a
Hi,

Guess I was not looking hard enough. ;-)

For Intel compilers, the subroutine to convert character to real is dnum.

Thanks
  Reply With Quote

Old   March 15, 2007, 04:33
Default Re: Fortran direct input help
  #8
Markus Lummer
Guest
 
Posts: n/a
Sorry, forgot the new lines.

character text*(80)

double precision number

read(text,*) number
  Reply With Quote

Old   March 15, 2007, 09:37
Default Re: Fortran direct input help
  #9
Joe
Guest
 
Posts: n/a
Wow, I didn't know this is possible too. It's better than "dnum" cos any compiler can use it.

Thanks again
  Reply With Quote

Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Fortran Compiler-CFX12.1 Araz CFX 13 March 27, 2017 06:37
subroutine FORTRAN // CFX in Spain spain CFX 0 May 23, 2006 07:12
Intrinsic Procedure 'ISNAN' in GNU FORTRAN 77 hawk Main CFD Forum 1 April 12, 2005 23:13
visual fortran Monica Main CFD Forum 1 August 28, 2004 21:45
Why Favoring Fortran over C/C++? Zi-Wei Chiou Main CFD Forum 35 September 26, 2001 10:34


All times are GMT -4. The time now is 23:26.