I have a .dat file with a series of data in many rows as shown bellow. I want to read two numbers from each row (2nd and 3rd number) and assign it to variables, such that the 2nd to x(1, i) and 3rd to x(2, i). The value of i changes from 1 to 9000 corresponding to each, 9000 rows in total.
1 0.2077742E-01 0.1315710E-01 0.2218703E-04 -0.7526844E-05
2 0.2064923E-01 0.1370921E-01 0.5160497E-05 -0.6106872E-04
3 0.2069335E-01 0.1446493E-01 -0.1517477E-04 -0.2678836E-04
4 0.2136012E-01 0.1310226E-01 0.8096579E-04 -0.2091828E-04
5 0.2129221E-01 0.1380866E-01 0.9713367E-05 -0.2111076E-04
6 0.2130786E-01 0.1449554E-01 -0.1372658E-05 -0.1607569E-05
7 0.2195375E-01 0.1304382E-01 0.1206853E-03 -0.2769564E-04
8 0.2171398E-01 0.1372358E-01 -0.1436741E-04 -0.2231794E-04
9 0.2193212E-01 0.1446982E-01 -0.5678712E-05 -0.1880452E-04
10 0.2255034E-01 0.1298457E-01 0.2226397E-03 -0.4775016E-04
11 0.2234811E-01 0.1370892E-01 0.1539219E-04 -0.2676827E-04
12 0.2250139E-01 0.1449063E-01 0.6857453E-05 -0.1959816E-04
13 0.2311584E-01 0.1292939E-01 0.3245234E-03 -0.2145422E-04
14 0.2265567E-01 0.1367124E-01 -0.5482677E-04 -0.6929516E-04
15 0.2308732E-01 0.1441105E-01 -0.4883980E-04 -0.5488964E-04
16 0.2369612E-01 0.1286988E-01 0.3164438E-03 0.5181705E-05
17 0.2333626E-01 0.1358709E-01 0.2992323E-04 -0.3658970E-04
18 0.2351967E-01 0.1444346E-01 0.2000859E-04 0.5844122E-05
19 0.2425562E-01 0.1280576E-01 0.1712960E-03 0.3221714E-04
20 0.2358314E-01 0.1359597E-01 -0.5286794E-04 0.5639317E-04
21 0.2406372E-01 0.1434354E-01 -0.3155423E-04 0.2389453E-04
22 0.2440915E-01 0.1444667E-01 0.9195025E-04 0.2545742E-04
23 0.2484257E-01 0.1274295E-01 0.6371955E-04 0.1572621E-04
24 0.2444211E-01 0.1359181E-01 -0.1755666E-03 0.1667949E-03
25 0.2431473E-01 0.1346151E-01 0.1424003E-03 -0.1036167E-03
26 0.2498710E-01 0.1434537E-01 0.3143868E-04 -0.4613371E-05
27 0.2542195E-01 0.1267686E-01 -0.1103745E-04 -0.3899099E-05
28 0.2527807E-01 0.1340544E-01 -0.1526311E-03 -0.1358241E-04
29 0.2519788E-01 0.1350556E-01 0.1004352E-03 0.6685333E-04
30 0.2528750E-01 0.1443648E-01 0.1048803E-03 0.2594530E-04
31 0.2605581E-01 0.1262580E-01 0.3114293E-04 0.5790992E-05
32 0.2602784E-01 0.1324524E-01 -0.6518681E-05 -0.1118536E-03
33 0.2583167E-01 0.1405316E-01 -0.9194989E-05 0.5246043E-04
34 0.2599337E-01 0.1449096E-01 0.1606246E-03 0.2065522E-04
35 0.2665012E-01 0.1256468E-01 0.4131713E-04 0.8817949E-05
36 0.2623754E-01 0.1339113E-01 -0.1785680E-03 0.1278207E-03
I wrote a code as shown bellow, but its not doing what I expected.
I don't know the meaning of each part of this code, such as STATUS='OLD', the meaning * in read command. I just followed some tutorial and trying to code similarly. If possible please explain them as well or refer me to a good, easy and comprehensive book or tutorial.
implicit none
integer:: i,a
real(8):: x(2,9000)
open (2, file="f_xv0950.dat",STATUS='OLD')
do i=1,9000
read (2,*) a, x(1,i), x(2,i)
enddo
close(2)
The outcome that I expect should look like,
x(1,1) = 0.2077742E-01 x(2,1) = 0.1315710E-01
x(1,2) = 0.2064923E-01 x(2,2) = 0.1370921E-01
x(1,3) = 0.2069335E-01 x(2,3) = 0.1446493E-01
and so on....
Your help is much appreciated. Thanks.
!!!!!!!!!!!! Edit_1 !!!!!!!!!!! Added the following lines to write the output to a file with the following code,
open (3,file="tempout.dat")
do i=1,9000
write (2,100) x(1,i), x(2,i)
enddo
100 format(2(2x, e14.7))
close (3)
But I am getting an error as
forrtl: severe (24): end-of-file during read, unit 2, file C:\Users\mkha0038\Desktop\XDSPH_RMC_T\f_xv0950.dat
Can you please help.
What you have written will almost work. Aside from the missing end do, you need to explicitly declare the variable x so that it is dimensioned as an array. Here is an example that works with your truncated example file with 36 lines:
program readin
real, dimension(2,36) :: x
integer :: i
open (2, file="f_xv0950.dat",STATUS='OLD')
do i=1,36
read (2,*) a, x(1,i), x(2,i)
end do
close(2)
do i=1,3
write(6,'(a,i1,a,e13.7,a,i1,a,e13.7)') "x(1,",i,") = ",x(1,i), &
" x(2,",i,") = ",x(2,i)
end do
end program readin
It gives output of
x(1,1) = 0.2077742E-01 x(2,1) = 0.1315710E-01
x(1,2) = 0.2064923E-01 x(2,2) = 0.1370921E-01
x(1,3) = 0.2069335E-01 x(2,3) = 0.1446493E-01
as you wish
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.