Csv Fortran只读取文件的第一条记录,然后遇到过早的EOF

Csv Fortran只读取文件的第一条记录,然后遇到过早的EOF,csv,fortran,eof,Csv,Fortran,Eof,我在从Matlab生成的csv文件中读取数据时遇到了一个小问题 首先是一些一般信息: filename='positie_frame_cam' extension='.txt' do icam=1,6,1 write (getal,'(I1)') icam ! converting integer to string using an 'internal file' totalname=filename//getal//extension print*,totalname

我在从Matlab生成的csv文件中读取数据时遇到了一个小问题

首先是一些一般信息:

filename='positie_frame_cam'
extension='.txt'
do icam=1,6,1
    write (getal,'(I1)') icam ! converting integer to string using an 'internal file'
    totalname=filename//getal//extension
    print*,totalname
    open(unit=icam+10,file=totalname, form='formatted',action='read')
    nlines(icam)=0
    iop=0
    i=0
    do while (iop.ge.0)
      i=i+1
      write(0,*)' regel nummer ',i
      read(icam+10,*,iostat=iop)tel,DUMY(1),DUMY(2),& 
      DUMY(3),DUMY(4),DUMY(5),DUMY(6),DUMY(7)
      write(0,*) 'iostat=',iop
      write(0,*) tel,DUMY(1),DUMY(2),&
      DUMY(3),DUMY(4),DUMY(5),DUMY(6),DUMY(7)
      nlines(icam)=nlines(icam)+1
      if (iop.ge.0) then
         nlines(icam)=nlines(icam)-1
         write(0,*) 'something went wrong!'
         close(icam+10)
      endif
      if (iop.eq.-1)then
         write(0,*)'eof'
         close(icam+10)
         exit
      endif
    end do
end do
1,0.00000000,0.36611265,0.05310654,0.60107656,-0.02129885,0.99435631,-0.10393212
1,0.00833892,0.36611265,0.05310654,0.60107656,-0.02129885,0.99435631,-0.10393212
1,0.01667784,0.36486442,0.05303874,0.59982310,-0.02163688,0.99431165,-0.10428895
1,0.02501677,0.35989577,0.05282108,0.59855295,-0.02244646,0.99420581,-0.10512358
1,0.03335569,0.35492529,0.05268669,0.59727332,-0.02325602,0.99409861,-0.10595804
1,0.04169461,0.35368452,0.05266613,0.59726437,-0.02341294,0.99407797,-0.10611699
1,0.05003353,0.35491671,0.05268653,0.59602339,-0.02343777,0.99407317,-0.10615646
1,0.05837246,0.35862988,0.05277895,0.59479665,-0.02314764,0.99410979,-0.10587683
1,0.06671138,0.36359068,0.05297500,0.59481924,-0.02251773,0.99419203,-0.10523858
1,0.07505030,0.36483061,0.05303698,0.59482341,-0.02236010,0.99421248,-0.10507885
Last login: Thu Mar 12 09:19:00 on ttys001
/Users/fclemens/eclipse-workspace/Fit_ellipsoid/Release/Fit_ellipsoid ; exit;

The default interactive shell is now zsh.
To update your account to use zsh, please run `chsh -s /bin/zsh`.
For more details, please visit https://support.apple.com/kb/HT208050.
(base) wlan-145-94-195-39:~ fclemens$ /Users/fclemens/eclipse-workspace/Fit_ellipsoid/Release/Fit_ellipsoid ; exit;
 positie_frame_cam1.txt
  regel nummer            1
 iostat=           0
  1   0.0000000000000000 0.36611264999999998  5.3106540000000001E-002  0.60107655999999998 -2.1298850000000001E-002  0.99435631000000002   -0.10393212000000000     
 something went wrong!
  regel nummer            2
 iostat=          -1
  1   0.0000000000000000 0.36611264999999998  5.3106540000000001E-002  0.60107655999999998 -2.1298850000000001E-002  0.99435631000000002   -0.10393212000000000     
 eof
操作系统:MacOSCatalina10.15.3 Fortran编译器:gfortran 9.2(2019年10月更新,据报道适用于Catalina OS)。 编程环境:Elcipse 2019-12(4.14.0)

下面是我遇到问题的代码片段:

filename='positie_frame_cam'
extension='.txt'
do icam=1,6,1
    write (getal,'(I1)') icam ! converting integer to string using an 'internal file'
    totalname=filename//getal//extension
    print*,totalname
    open(unit=icam+10,file=totalname, form='formatted',action='read')
    nlines(icam)=0
    iop=0
    i=0
    do while (iop.ge.0)
      i=i+1
      write(0,*)' regel nummer ',i
      read(icam+10,*,iostat=iop)tel,DUMY(1),DUMY(2),& 
      DUMY(3),DUMY(4),DUMY(5),DUMY(6),DUMY(7)
      write(0,*) 'iostat=',iop
      write(0,*) tel,DUMY(1),DUMY(2),&
      DUMY(3),DUMY(4),DUMY(5),DUMY(6),DUMY(7)
      nlines(icam)=nlines(icam)+1
      if (iop.ge.0) then
         nlines(icam)=nlines(icam)-1
         write(0,*) 'something went wrong!'
         close(icam+10)
      endif
      if (iop.eq.-1)then
         write(0,*)'eof'
         close(icam+10)
         exit
      endif
    end do
end do
1,0.00000000,0.36611265,0.05310654,0.60107656,-0.02129885,0.99435631,-0.10393212
1,0.00833892,0.36611265,0.05310654,0.60107656,-0.02129885,0.99435631,-0.10393212
1,0.01667784,0.36486442,0.05303874,0.59982310,-0.02163688,0.99431165,-0.10428895
1,0.02501677,0.35989577,0.05282108,0.59855295,-0.02244646,0.99420581,-0.10512358
1,0.03335569,0.35492529,0.05268669,0.59727332,-0.02325602,0.99409861,-0.10595804
1,0.04169461,0.35368452,0.05266613,0.59726437,-0.02341294,0.99407797,-0.10611699
1,0.05003353,0.35491671,0.05268653,0.59602339,-0.02343777,0.99407317,-0.10615646
1,0.05837246,0.35862988,0.05277895,0.59479665,-0.02314764,0.99410979,-0.10587683
1,0.06671138,0.36359068,0.05297500,0.59481924,-0.02251773,0.99419203,-0.10523858
1,0.07505030,0.36483061,0.05303698,0.59482341,-0.02236010,0.99421248,-0.10507885
Last login: Thu Mar 12 09:19:00 on ttys001
/Users/fclemens/eclipse-workspace/Fit_ellipsoid/Release/Fit_ellipsoid ; exit;

The default interactive shell is now zsh.
To update your account to use zsh, please run `chsh -s /bin/zsh`.
For more details, please visit https://support.apple.com/kb/HT208050.
(base) wlan-145-94-195-39:~ fclemens$ /Users/fclemens/eclipse-workspace/Fit_ellipsoid/Release/Fit_ellipsoid ; exit;
 positie_frame_cam1.txt
  regel nummer            1
 iostat=           0
  1   0.0000000000000000 0.36611264999999998  5.3106540000000001E-002  0.60107655999999998 -2.1298850000000001E-002  0.99435631000000002   -0.10393212000000000     
 something went wrong!
  regel nummer            2
 iostat=          -1
  1   0.0000000000000000 0.36611264999999998  5.3106540000000001E-002  0.60107655999999998 -2.1298850000000001E-002  0.99435631000000002   -0.10393212000000000     
 eof
下面是我试图读取的6个文件的示例:

filename='positie_frame_cam'
extension='.txt'
do icam=1,6,1
    write (getal,'(I1)') icam ! converting integer to string using an 'internal file'
    totalname=filename//getal//extension
    print*,totalname
    open(unit=icam+10,file=totalname, form='formatted',action='read')
    nlines(icam)=0
    iop=0
    i=0
    do while (iop.ge.0)
      i=i+1
      write(0,*)' regel nummer ',i
      read(icam+10,*,iostat=iop)tel,DUMY(1),DUMY(2),& 
      DUMY(3),DUMY(4),DUMY(5),DUMY(6),DUMY(7)
      write(0,*) 'iostat=',iop
      write(0,*) tel,DUMY(1),DUMY(2),&
      DUMY(3),DUMY(4),DUMY(5),DUMY(6),DUMY(7)
      nlines(icam)=nlines(icam)+1
      if (iop.ge.0) then
         nlines(icam)=nlines(icam)-1
         write(0,*) 'something went wrong!'
         close(icam+10)
      endif
      if (iop.eq.-1)then
         write(0,*)'eof'
         close(icam+10)
         exit
      endif
    end do
end do
1,0.00000000,0.36611265,0.05310654,0.60107656,-0.02129885,0.99435631,-0.10393212
1,0.00833892,0.36611265,0.05310654,0.60107656,-0.02129885,0.99435631,-0.10393212
1,0.01667784,0.36486442,0.05303874,0.59982310,-0.02163688,0.99431165,-0.10428895
1,0.02501677,0.35989577,0.05282108,0.59855295,-0.02244646,0.99420581,-0.10512358
1,0.03335569,0.35492529,0.05268669,0.59727332,-0.02325602,0.99409861,-0.10595804
1,0.04169461,0.35368452,0.05266613,0.59726437,-0.02341294,0.99407797,-0.10611699
1,0.05003353,0.35491671,0.05268653,0.59602339,-0.02343777,0.99407317,-0.10615646
1,0.05837246,0.35862988,0.05277895,0.59479665,-0.02314764,0.99410979,-0.10587683
1,0.06671138,0.36359068,0.05297500,0.59481924,-0.02251773,0.99419203,-0.10523858
1,0.07505030,0.36483061,0.05303698,0.59482341,-0.02236010,0.99421248,-0.10507885
Last login: Thu Mar 12 09:19:00 on ttys001
/Users/fclemens/eclipse-workspace/Fit_ellipsoid/Release/Fit_ellipsoid ; exit;

The default interactive shell is now zsh.
To update your account to use zsh, please run `chsh -s /bin/zsh`.
For more details, please visit https://support.apple.com/kb/HT208050.
(base) wlan-145-94-195-39:~ fclemens$ /Users/fclemens/eclipse-workspace/Fit_ellipsoid/Release/Fit_ellipsoid ; exit;
 positie_frame_cam1.txt
  regel nummer            1
 iostat=           0
  1   0.0000000000000000 0.36611264999999998  5.3106540000000001E-002  0.60107655999999998 -2.1298850000000001E-002  0.99435631000000002   -0.10393212000000000     
 something went wrong!
  regel nummer            2
 iostat=          -1
  1   0.0000000000000000 0.36611264999999998  5.3106540000000001E-002  0.60107655999999998 -2.1298850000000001E-002  0.99435631000000002   -0.10393212000000000     
 eof
这是一个文件的输出:

filename='positie_frame_cam'
extension='.txt'
do icam=1,6,1
    write (getal,'(I1)') icam ! converting integer to string using an 'internal file'
    totalname=filename//getal//extension
    print*,totalname
    open(unit=icam+10,file=totalname, form='formatted',action='read')
    nlines(icam)=0
    iop=0
    i=0
    do while (iop.ge.0)
      i=i+1
      write(0,*)' regel nummer ',i
      read(icam+10,*,iostat=iop)tel,DUMY(1),DUMY(2),& 
      DUMY(3),DUMY(4),DUMY(5),DUMY(6),DUMY(7)
      write(0,*) 'iostat=',iop
      write(0,*) tel,DUMY(1),DUMY(2),&
      DUMY(3),DUMY(4),DUMY(5),DUMY(6),DUMY(7)
      nlines(icam)=nlines(icam)+1
      if (iop.ge.0) then
         nlines(icam)=nlines(icam)-1
         write(0,*) 'something went wrong!'
         close(icam+10)
      endif
      if (iop.eq.-1)then
         write(0,*)'eof'
         close(icam+10)
         exit
      endif
    end do
end do
1,0.00000000,0.36611265,0.05310654,0.60107656,-0.02129885,0.99435631,-0.10393212
1,0.00833892,0.36611265,0.05310654,0.60107656,-0.02129885,0.99435631,-0.10393212
1,0.01667784,0.36486442,0.05303874,0.59982310,-0.02163688,0.99431165,-0.10428895
1,0.02501677,0.35989577,0.05282108,0.59855295,-0.02244646,0.99420581,-0.10512358
1,0.03335569,0.35492529,0.05268669,0.59727332,-0.02325602,0.99409861,-0.10595804
1,0.04169461,0.35368452,0.05266613,0.59726437,-0.02341294,0.99407797,-0.10611699
1,0.05003353,0.35491671,0.05268653,0.59602339,-0.02343777,0.99407317,-0.10615646
1,0.05837246,0.35862988,0.05277895,0.59479665,-0.02314764,0.99410979,-0.10587683
1,0.06671138,0.36359068,0.05297500,0.59481924,-0.02251773,0.99419203,-0.10523858
1,0.07505030,0.36483061,0.05303698,0.59482341,-0.02236010,0.99421248,-0.10507885
Last login: Thu Mar 12 09:19:00 on ttys001
/Users/fclemens/eclipse-workspace/Fit_ellipsoid/Release/Fit_ellipsoid ; exit;

The default interactive shell is now zsh.
To update your account to use zsh, please run `chsh -s /bin/zsh`.
For more details, please visit https://support.apple.com/kb/HT208050.
(base) wlan-145-94-195-39:~ fclemens$ /Users/fclemens/eclipse-workspace/Fit_ellipsoid/Release/Fit_ellipsoid ; exit;
 positie_frame_cam1.txt
  regel nummer            1
 iostat=           0
  1   0.0000000000000000 0.36611264999999998  5.3106540000000001E-002  0.60107655999999998 -2.1298850000000001E-002  0.99435631000000002   -0.10393212000000000     
 something went wrong!
  regel nummer            2
 iostat=          -1
  1   0.0000000000000000 0.36611264999999998  5.3106540000000001E-002  0.60107655999999998 -2.1298850000000001E-002  0.99435631000000002   -0.10393212000000000     
 eof

显然,记录1读取成功,但随后再次读取第一条记录而不是第二条记录,并检测到eof。我不知道如何解决这个问题。

您的代码显然写了
出了问题在输出示例中

     write(0,*) 'something went wrong!'
     close(icam+10)
这意味着文件已关闭,然后它再次尝试从已关闭的单元读取,但无法正常工作

很可能您想在中使用
.gt.
而不是
.ge.

if (iop.ge.0) then
然而,我强烈地建议改用
。它更具可读性,你更有可能发现问题,而不必在互联网上提问


在此之后,您将需要在此if分支中的
关闭
语句之后执行
退出

您的代码清楚地写入
某些错误在输出示例中

     write(0,*) 'something went wrong!'
     close(icam+10)
这意味着文件已关闭,然后它再次尝试从已关闭的单元读取,但无法正常工作

很可能您想在中使用
.gt.
而不是
.ge.

if (iop.ge.0) then
然而,我强烈地建议改用
。它更具可读性,你更有可能发现问题,而不必在互联网上提问


在此之后,您将需要在if分支中的
close
语句之后执行
exit

欢迎,请务必执行以下操作。请对所有Fortran问题使用tag。您可以向特定于版本的问题添加特定的版本标记。如果出现错误或某个条件结束,则读取的值可能未定义。您不能确定它是否真的再次读取了第一条记录,这可能是一种转移注意力的做法。欢迎使用,请务必使用。请对所有Fortran问题使用tag。您可以向特定于版本的问题添加特定的版本标记。如果出现错误或某个条件结束,则读取的值可能未定义。你不能确定它是否真的再次读取了第一条记录,这可能是一种误导。非常感谢,这是有效的,确实是我这边的一个愚蠢错误(但在>25年后,我的Fortran有点生锈)。。。。。不管怎样,你救了我一天!!!!!!非常感谢,这是可行的,事实上,这是我这边的一个愚蠢错误(但在>25年后,我的Fortran有点生锈)。。。。。不管怎样,你救了我一天!!!!!!