Fortran What does this Fortran Line Mean?

AI Thread Summary
The Fortran line "read(A, *) B" indicates that the program reads input from the unit specified by A, which defaults to the keyboard if A equals 5. The second asterisk signifies that the default format for the input data will be used, typically ASCII characters. When using "read(10, '(10x, 4(3x,f8.4, 3x))')", it reads from unit 10, which is associated with a file opened earlier in the code, and the format specifies the structure of the expected input. The discussion also clarifies that slashes in write statements act like "enter" keys for formatting output. Overall, understanding these syntax elements is crucial for effective data input and output in Fortran programming.
ecastro
Messages
249
Reaction score
8
This is the line:
Code:
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:
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.
 
Technology news on Phys.org
5 and 6 are the usual numbers for terminal I/O.
Code:
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.
 
I searched the value of A and it is equal to five. Does that mean it needs an input from the keyboard?
 
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.
 
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
 
  • Like
Likes jedishrfu
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?
 
ecastro said:
open(10, file = FILE)
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.

ecastro said:
Thank you. Here is the same syntax with a different input:
read(10, '(10x, 4(3x,f8.4, 3x))')
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.
 
DrClaude said:
4 times 3 blanks followed by a floating point number of size 8 with 4 digits after the decimal point, followed by three blanks

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?
 
  • #10
ecastro said:
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?
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:
^^^^^^^^^^^^^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:
             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
 
  • #11
Thank you. How about this line:

Code:
write(10,'(//,T20,A16,/,3x,A4,1x,20(3x,f6.4,2x))')

And what does the secondary input 'e10.4' mean?
 
  • #12
ecastro said:
How about this line:

Code:
write(10,'(//,T20,A16,/,3x,A4,1x,20(3x,f6.4,2x))')

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

And what does the secondary input 'e10.4' mean?

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:
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:
  • #13
Thank you! That is more than I wanted to know. :biggrin:
 

Similar threads

Replies
3
Views
2K
Replies
16
Views
2K
Replies
39
Views
367
Replies
59
Views
11K
Replies
6
Views
7K
Replies
5
Views
10K
Replies
5
Views
2K
Replies
4
Views
2K
Back
Top