Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Fortran What does this Fortran Line Mean?

Tags:
  1. Jul 28, 2016 #1
    This is the line:
    Code (Text):
    read(A, *) B
    The variable A is defined earlier in the code. I searched around the internet, and the 'read' syntax requires an input from the keyboard. However, the syntax I saw is
    Code (Text):
    read(*, *) var
    , so the two arguments are both asterisks. What if the first asterisk is defined, does it still require an input from the keyboard, and what does the second asterisk mean?

    Thank you in advance.
     
  2. jcsd
  3. Jul 28, 2016 #2

    jim mcnamara

    User Avatar

    Staff: Mentor

    5 and 6 are the usual numbers for terminal I/O.
    Code (Text):
    read(xx, yy)
    -- read from unit xx, format the input with the yy format.
    First * is the default unit - keyboard, second * means use the default format for the unit, usually ASCII characters.

    What do you mean by 'first asterisk is defined' - I don't think so, we do not get to define the asterisk.
    If you mean the first asterisk is a unit number instead of an asterisk, then a second asterisk means 'default' format for that unit. In other words it is system dependent. Possibly not the best idea either. IMO.
     
  4. Jul 28, 2016 #3

    jedishrfu

    Staff: Mentor

  5. Jul 28, 2016 #4
    I searched the value of A and it is equal to five. Does that mean it needs an input from the keyboard?
     
  6. Jul 28, 2016 #5

    jedishrfu

    Staff: Mentor

    Yes, five is the default device so if you're running your fortran interactively as most folks do now then 5 is the keyboard. However, if you were running a batch job then 5 means the card input.
     
  7. Jul 28, 2016 #6

    jtbell

    User Avatar

    Staff: Mentor

    On a Unix-like system unit 5 is probably "standard input" so instead of using the keyboard you can also redirect the input at the command line, to come from a file:

    ./yourprogram < inputfile
     
  8. Aug 2, 2016 #7
    Thank you. Here is the same syntax with a different input:

    read(10, '(10x, 4(3x,f8.4, 3x))')

    The first argument 10 came from here:

    open(10, file = FILE)

    I looked through the web to know the definitions of the syntax 'open'. Does this mean that the 'FILE' is stored in unit 10? And from the 'read' syntax, the unit 10 is read? Also, what is the meaning of the second argument? I know that it is for a format; what does it mean?
     
  9. Aug 2, 2016 #8

    DrClaude

    User Avatar

    Staff: Mentor

    FILE has to be a variable of type character. This will open the file whose name is contained in FILE and assign it to unit 10.

    This will read from unit 10 (hence, from file FILE) first a series of 10 blanks, then 4 times 3 blanks followed by a floating point number of size 8 with 4 digits after the decimal point, followed by three blanks.

    Note that there has to be 4 variables specified with that read, or an array of a least size 4.
     
  10. Aug 4, 2016 #9
    Did you mean that there are 12 blanks followed by 4 floating point numbers of size 8 with 4 digits after the decimal point, and then followed by 12 blanks; or there are 3 blanks, a floating point number of size 8 with 4 digits after the decimal point and another three blanks, which are repeated four times?
     
  11. Aug 4, 2016 #10

    DrClaude

    User Avatar

    Staff: Mentor

    The latter. That should have been obvious from the format '(10x, 4(3x,f8.4, 3x))', where the 4 "multiplies" the parenthesis.


    Explicitly, the input would have to look like
    Code (Text):

    ^^^^^^^^^^^^^123.4567^^^^^^123.4567^^^^^^-12.3456^^^^^ 12.3456
     
    where the ^ stand for blanks or ignored input. For instance, the following three lines will be read without errors and result in the same input, namely the numbers 123.4567, 123.4567, -12.3456, and 12.3456:
    Code (Text):

                 123.4567      123.4567      -12.3456       12.3456
    line 1       123.4567      123.4567      -12.3456       12.3456
    line B       123.4567      123.4567      -12.3456       12.3456
                 123.4567     9123.4567      -12.3456       12.3456
     
     
  12. Aug 22, 2016 #11
    Thank you. How about this line:

    Code (Text):
    write(10,'(//,T20,A16,/,3x,A4,1x,20(3x,f6.4,2x))')
    And what does the secondary input 'e10.4' mean?
     
  13. Aug 22, 2016 #12

    jtbell

    User Avatar

    Staff: Mentor

    Are you referring to the slashes "/"? Think of them as equivalent to hitting the "enter" or "return" key in keyboard output or input. I think in this case, the first two lines of data are blank on output and ignored (skipped) on input, the third has format 'T20,A16', and the fourth has format '3x,A4,1x,20(3x,f6.4,2x)'.

    E = "exponential" format, i.e. so-called "scientific notation." E10.4 means an input field with a total width of 10 chars, with 4 digits after the decimal point, e.g.

    0.1234E+03
    1.2345E-07

    [addition #2] Argh! :H The three paragraphs below don't work with GNU fortran (gfortran), now that I've had a chance to try it. You have to specify a width in a format specification. Either I misremembered it, or else I was using a non-standard extension to Fortran 77. However, you can get the same result by using * instead of a format statement number in the 'read' statement, so as to use free-form format:

    read (10,*) a, b, c, d

    The * format is in fact what I normally used for input, unless I was forced into using an explicit format because of some quirk in the layout of the data file. For example, if your data is in columns, and some of the entries are completely blank (not even a 0), you have to use an explicit format so that the blanks actually get read as 0.

    ----- begin non-standard stuff -----

    [added] As I recall, if your input data items are all separated by blank spaces, you don't need to use format specifications that include the width or number of decimal places. You can simply use the type letter alone, with a repetition count if necessary.

    For example, in post #10 above, I'm pretty sure you can use simply '(4f)' instead of '(10x, 4(3x,f8.4, 3x))' to read the first example input, or the first and fourth lines of the second example. It would not work for the second and third lines of the second example, because of the extra stuff at the beginning of the line. However, '(10x, 4f') would read all four lines, ignoring the extra stuff.

    For four numbers in exponential notation, '(4e)'. For two integers followed by three floating-point, followed by an exponential, '(4i, 3f, e)'. Simply separate the input numbers with at least one blank space.

    ------ end non-standard stuff ------

    On input, the detailed specifications are useful if the numbers "run together" without blank spaces separating them. You can also even omit the decimal point, which was often done to save space and data-entry time in the days when 80-column punched cards were the norm for input. (Yes, I remember those days, just barely!) In commercial applications, the cards were often pre-printed with solid and dashed lines to separate the fields and locate the decimal points visually, and with some text along the top of the card to identify the fields, so that the keypunch operators could easily proofread their work.

    ibm701.gif

    For example, on a punched card, you could enter the four numbers in post #10 as

    Code (Text):

    12345671234567-123456 123456
     
    and read them using the format ('4f7.4'), getting the result that DrClaude indicated. You can do the same with your input data files if you want to mystify whoever reads them. :biggrin:
     
    Last edited: Aug 22, 2016
  14. Aug 30, 2016 #13
    Thank you! That is more than I wanted to know. :biggrin:
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted



Similar Discussions: What does this Fortran Line Mean?
Loading...