Fortran:打开,form ='(un)格式化',读取

时间:2013-11-12 02:20:58

标签: file-io import format fortran

我的数据('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. 工人数量。
  5. 我的目标是从外部数据中获取四个向量。我正在编写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大师寻求帮助了。如果你能帮助我解决这些错误,我将非常感激。

1 个答案:

答案 0 :(得分:1)

我删除了form=unformatted语句的open部分,但通过该部分没问题。

修复上述内容后,我收到了不同的错误。在分配变量之后,您尝试从文件中读取而不将其重绕。也就是说,程序最后将文件保留在最后,因此当您尝试阅读时,没有更多内容可供阅读。修复很简单:告诉程序使用rewind

跳回到开头
allocate(jobs(nr))
rewind(10)
do i=1,nr
   read(10,*) ...
enddo