What does this Fortran Line Mean?

In summary: T20,A16,/,3x,A4,1x,20(3x,f6.4,2x))')This will write the string '(//,T20,A16,/,3x,A4,1x,20(3x,f6.4,2x))' to unit 10.
  • #1
ecastro
254
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
  • #2
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.
 
  • #4
I searched the value of A and it is equal to five. Does that mean it needs an input from the keyboard?
 
  • #5
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.
 
  • #6
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
  • #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?
 
  • #8
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.
 
  • #9
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:
 

Related to What does this Fortran Line Mean?

1. What is Fortran?

Fortran is a high-level programming language commonly used for scientific and engineering applications. It was first developed in the 1950s and is still widely used today.

2. What does the syntax of a Fortran line mean?

The syntax of a Fortran line describes the structure and format of the line of code, including the use of keywords, variables, and operators. It is important to follow the correct syntax in order for the code to be executed correctly.

3. How do I read a Fortran line of code?

Fortran code is read from left to right, with each statement or instruction separated by a line break. It is important to understand the meaning of each part of the code, such as the variables and operators, in order to understand what the line is doing.

4. How do I debug a Fortran line of code?

Debugging a Fortran line of code involves identifying and fixing errors in the code that prevent it from running correctly. This can be done by using a debugger tool, carefully reviewing the code, and checking for common mistakes such as typos or incorrect syntax.

5. Can I use comments in Fortran lines of code?

Yes, comments can be used in Fortran code to provide notes and explanations for the code. Comments are preceded by an exclamation mark (!) and are ignored by the compiler, so they do not affect the execution of the code.

Similar threads

  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
2
Replies
59
Views
9K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Programming and Computer Science
Replies
6
Views
6K
  • Programming and Computer Science
Replies
2
Views
7K
  • Programming and Computer Science
Replies
17
Views
4K
  • Programming and Computer Science
Replies
3
Views
360
  • Programming and Computer Science
Replies
6
Views
920
  • Programming and Computer Science
Replies
5
Views
10K
  • Programming and Computer Science
Replies
20
Views
5K
Back
Top