Fortran:打开,form ='(un)formatted',阅读 [英] Fortran: Open, form='(un)formatted', read
问题描述
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
100030 002001016 39.767773 -75.523119 2
100030002001017 39.76735 -75.522105 0
100030002001018 39.768074 -75.521028 12
100030002001019 39.766908 -75.521198 0
$ b
变量是
$ b
- 唯一键(len = 15),
- 经度
- 纬度
- 工作人员数量。
我的目标是从外部数据中获取四个向量。我正在编写一个Fortran代码,如下所示:
$ $ $ $ $ $
$ b整数,参数:: maxnr = 200000
整数:: nr,i,j,ios
字符(len = 1):: junkfornr
!我的变量声明
character(len = 15),dimension(:),allocatable :: key
real,dimension(:),allocatable :: lat,lon
integer,dimension(:) ,allocatable :: jobs
!确定我的数据文件中的总行数
nr = 0
open(10,file ='location_data.txt',status ='old',form ='formatted')
do i = 1,maxnr
read(10,*,iostat = ios)junkfornr
如果(ios / = 0)退出
if(i == maxnr)then
write *,*)错误:对不起,最大记录数超过了...
stop
endif
nr = nr + 1
end do
print *,数据的行数=,nr
!创建变量:key,lat,lon,jobs
allocate(key(nr))
allocate(lat(nr))
allocate(lon(nr))
allocate (nr))
!从数据文件读取变量
do i = 1,nr
read(10,*)key(i),lat(i),lon(i),jobs(i)
end做
$ b $打印*,键
关闭(10)
结束程序位置
$如果我在open语句中使用 form ='formatted',我得到了在屏幕上显示以下消息:
pre $ $ $ $ $ $ $ $ $ $ $ $ $ $ $在文件位置的第41行.f90< unit = 10,file ='location_data.txt'>
Fortran运行时错误:在EOF市场之后不允许使用顺序READ或WRITE,
可能使用REWIND或BACKSPACE
当我将 form ='formatted'更改为 form ='unformatted'时,我得到了另一个错误的结果:
数据的行数= 0
为了在Fortran中获得更大数据分析的解决方案,我有几个障碍需要解决,我认为现在是时候向Fortran大师求助了。如果你能帮我解决这些错误,我将不胜感激。
我删除了 form = unformatted
$ c>打开语句,它通过该部分没有问题。
修复上述后,我收到了一个不同的错误。分配变量之后,您可以尝试从文件中读取而不倒带 。也就是说,程序最后把文件留在最后,所以当你尝试阅读时没有更多的东西需要阅读。修复很简单:通过使用 rewind
告诉程序跳回到开头:
(10,*)...
enddo $($)$ $ $ $ $ $ $ $
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
- unique key (len=15),
- longitude
- latitude
- 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.
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
这篇关于Fortran:打开,form ='(un)formatted',阅读的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!