1

我的数据('location_data.txt')如下所示:

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

变量是

  1. 唯一键(len=15),
  2. 经度
  3. 纬度
  4. 工人的数量。

我的目标是从外部数据中获取四个向量。我正在编写如下 Fortran 代码:

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

如果我在 open 语句中使用form='formatted',我会在屏幕上看到以下消息:

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

当我将form='formatted'更改为form='unformatted'时,我有另一个错误的结果:

 The number of rows of the data =                 0

为了在 Fortran 中获得更大数据分析的解决方案,我有几个障碍需要解决,我认为现在是时候向 Fortran 专家寻求帮助了。如果您能帮助我解决这些错误,我将不胜感激。

4

1 回答 1

1

我删除了声明的form=unformatted一部分,open它通过了那部分没有问题。

修复上述问题后,我收到了另一个错误。分配变量后,您尝试从文件中读取而不倒回它。也就是说,程序最后将文件放在最后,因此当您尝试读取它时,没有更多内容可读取。修复很简单:告诉程序使用以下命令跳回到开头rewind

allocate(jobs(nr))
rewind(10)
do i=1,nr
   read(10,*) ...
enddo
于 2013-11-12T02:29:58.133 回答