File io Fortran:Open,form=';(未)格式化';,阅读

File io Fortran:Open,form=';(未)格式化';,阅读,file-io,import,format,fortran,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 10003000

我的数据('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
变量是

  • 唯一键(len=15)
  • 经度
  • 纬度
  • 工人的数量
  • 我的目标是从外部数据中得到四个向量。我正在编写一个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
    

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

    我删除了
    open
    语句的
    form=unformatted
    部分,该部分没有问题

    在修复上述问题后,我收到了不同的错误。分配变量后,您可以尝试从文件中读取,而无需重绕文件。也就是说,程序最后一次将文件保留在末尾,因此当您尝试读取它时,没有更多的内容可读取。解决方法很简单:告诉程序使用倒带跳回到开始:

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

    我删除了
    open
    语句的
    form=unformatted
    部分,它顺利通过了该部分

    在修复上述问题后,我收到了不同的错误。分配变量后,您可以尝试从文件中读取,而无需重绕文件。也就是说,程序最后一次将文件保留在末尾,因此当您尝试读取它时,没有更多的内容可读取。解决方法很简单:告诉程序使用倒带跳回到开始:

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

    谢谢如果我在结束后加上倒带(10)行吗?凯尔,它行!我认为我找到了数据文件的行数,即nr,因此通过I=1到nr循环应该可以工作。我不明白倒带语句的真正作用。把它想象成录像带。为了第二次看电影,你必须倒带它。同样,为了读取数据(在读取数据之后),您需要
    倒带它。哦,你们这些幸运的孩子,想想用来存储数据的磁带吧。很多大数据商店也在使用。谢谢。如果我在结束后加上倒带(10)行吗?凯尔,它行!我认为我找到了数据文件的行数,即nr,因此通过I=1到nr循环应该可以工作。我不明白倒带语句的真正作用。把它想象成录像带。为了第二次看电影,你必须倒带它。同样,为了读取数据(在读取数据之后),您需要
    倒带它。哦,你们这些幸运的孩子,想想用来存储数据的磁带吧。许多大数据商店也在使用。