Fortran: Open, form='(un)formatted', read

2019-06-05 00:46发布

问题:

My data ('location_data.txt') look like :

100030002001000   39.772158  -75.527002         40  
100030002001001   39.771498  -75.525601         150  
100030002001002   39.771226  -75.526509         2  
100030002001003   39.767265  -75.526035         0  
100030002001004   39.769209  -75.527356         1  
100030002001005   39.769644  -75.528116         0  
100030002001006   39.767594  -75.527571         3  
100030002001007   39.770331  -75.530489         0  
100030002001008   39.769329  -75.529616         230  
100030002001009   39.768497  -75.528902         0  
100030002001010   39.769968  -75.524596         3  
100030002001011   39.769757  -75.525916         0  
100030002001012   39.768603  -75.525462         5  
100030002001013   39.768216  -75.524161         0  
100030002001014   39.768765  -75.522921         0  
100030002001015   39.767254  -75.524572         77  
100030002001016   39.767773  -75.523119         2  
100030002001017    39.76735  -75.522105         0  
100030002001018   39.768074  -75.521028         12  
100030002001019   39.766908  -75.521198         0

The variables are

  1. unique key (len=15),
  2. longitude
  3. latitude
  4. the number of workers.

My goal here is to get the four vectors from the external data. I am writing a Fortran code as follows:

program location

implicit none

integer, parameter :: maxnr = 200000
integer :: nr, i, j, ios
character(len=1) :: junkfornr

! My variable declaration
character(len=15), dimension(:), allocatable :: key
real, dimension(:), allocatable :: lat, lon
integer, dimension(:), allocatable :: jobs

! Determine the total number of lines in my data file
nr=0
open(10, file='location_data.txt', status='old', form='formatted')
do i=1,maxnr
    read(10,*,iostat=ios) junkfornr
    if (ios/=0) exit
    if (i == maxnr) then
        write(*,*) "Error: Sorry, max. # of records exceeded..."
        stop
    endif
    nr = nr + 1
end do

print*, "The number of rows of the data =", nr

! Create variables: key, lat, lon, jobs
allocate(key(nr))
allocate(lat(nr))
allocate(lon(nr))
allocate(jobs(nr))

! Read variables from the data file
do i=1,nr
    read(10,*) key(i), lat(i), lon(i), jobs(i)
end do

print*, key

close(10)

end program location

If I use form='formatted' in the open statement, I've got the following message on the screen:

The number of rows of the data =                 20
At line 41 of file location.f90 <unit=10, file = 'location_data.txt'>
Fortran runtime error: Sequential READ or WRITE not allowed after EOF market,
possibly use REWIND or BACKSPACE

And when I change form='formatted' to form='unformatted', I have another erroneous result:

 The number of rows of the data =                 0

I have several barriers to tackle in order to get to the solution for a bigger data analysis in Fortran, and I think now it's the time to seek some help from you Fortran gurus. I would really appreciate it if you could help me with these errors.

回答1:

I removed the form=unformatted part of the open statement and it got through that part no problem.

I received a different error after fixing the above. After allocating the variables, you try reading from the file without rewinding it. That is, the program last left the file at at the end, so there isn't anything more to read when you try to read it. The fix is simple: tell the program to jump back up to the beginning by using rewind:

allocate(jobs(nr))
rewind(10)
do i=1,nr
   read(10,*) ...
enddo