Store an Integer After Reading It From a File
Array Input/Output

Input
The easiest style of reading data into an array could be the following:In this example, if the input to n is v, the READ(*,*) statement in the Exercise-loop executes five times. Each time this READ(*,*) is executed, information technology reads in one line and consumes the start integer value of that line. Therefore, excluding the input for n, five input lines are required.INTEGER, DIMENSION(one:10) :: x INTEGER :: n, i READ(*,*) n Practice i = one, n READ(*,*) ten(i) Stop DO
However, the implied DO can simplify this greatly. Consider the following example:
If the value of northward is v, the implied Practice in the READ(*,*) is expanded toINTEGER, DIMENSION(1:10) :: x INTEGER :: n, i READ(*,*) n READ(*,*) (x(i), i=one, north)
Therefore, the READ(*,*) argument is actually equivalent tox(1), x(two), x(3), 10(iv), x(5)
So, what is the difference? This is simple. Call back that the execution of a READ(*,*) would consume at least one input line. As a result, the above READ(*,*) with implied Do tin can read in all five numbers on a single input line rather than on five separate lines. Moreover, this READ(*,*) tin can also read in the input on five separate lines, since Fortran volition automatically search for the next input value. Consider the post-obit two input files:INTEGER, DIMENSION(1:x) :: 10 INTEGER :: northward, i READ(*,*) 10(1), 10(2), x(3), x(4), x(5)
and the following two READ(*,*)s:File #ane File #2 ------- ------- 5 5 x 10 30 twenty forty 50 xxx 20 xl fifty
READ #1 tin certainly reads in File #1 correctly. The result isREAD #1 READ #two ======= ======= INTEGER,DIMENSION(1:10) :: x INTEGER,DIMENSION(1:10) :: ten INTEGER :: n, i INTEGER :: n, i READ(*,*) northward READ(*,*) due north Exercise i = 1, n READ(*,*) (x(i), i=1, n) READ(*,*) x(i) End Do
n | x(ane) | ten(two) | x(three) | ten(4) | x(five) |
5 | ten | 30 | 20 | twoscore | 50 |
Using READ #2 to read File #1 would get the same result. But, READ #1 will be able to read File #2. For READ #1, n still receives v. Then, to read in value for 10(one), a new line which contains all five integers is read. Since simply x(ane) needs a value, 10 is retrieved and all remaining values are ignored! When the DO loops back to read value for x(2), there is no input value in the input and an error occurs!
It is interesting to remind you that the following READ #3 can successfully read in File #1 and File #two. Why?
READ #3 ======= INTEGER, DIMENSION(1:10) :: x INTEGER :: north, i READ(*,*) n, (ten(i), i=1, n)

Output
One time yous know the way of using implied Do to do input, you would know the output counterpart. At that place is a divergence, though. For READ(*,*), items in implied DOs must be variables, since only variables can exist used in a READ(*,*). For WRITE(*,*), the items can be constants, variables, or expressions. The results of expressions are evaluated first, of grade. Consider the post-obit instance:The first WRITE(*,*) is executed five times. Each fourth dimension, information technology reads in 2 integers, one for x(i) and the other for y(i). Thus, the output is on v different lines.INTEGER, DIMENSION(one:10) :: x, y INTEGER :: n = 5, i DO i = 1, v WRITE(*,*) x(i), y(i) END Practise WRITE(*,*) (x(i), y(i), i=1, n)
The second WRITE(*,*) is equivalent to
Therefore, the 10 output values are on the same line.INTEGER, DIMENSION(i:x) :: x, y INTEGER :: n = 5, i WRITE(*,*) ten(i), y(one), x(2), y(2), 10(3), y(3), x(iv), y(4), 10(5), y(5)
silversteinhishat.blogspot.com
Source: https://pages.mtu.edu/~shene/COURSES/cs201/NOTES/chap08/array-io.html
Post a Comment for "Store an Integer After Reading It From a File"