【问题标题】:Incorrect reading of a variable from a txt (Fortran)从 txt (Fortran) 中错误读取变量
【发布时间】:2021-03-29 13:34:12
【问题描述】:

我正在尝试阅读此 txt:

Fecha   dia mes ano hora    min 

03/06/2016 00:00    3   6   2016    0   0   
03/06/2016 00:05    3   6   2016    0   5   
03/06/2016 00:10    3   6   2016    0   10  
03/06/2016 00:15    3   6   2016    0   15  
03/06/2016 00:20    3   6   2016    0   20  
03/06/2016 00:25    3   6   2016    0   25  
03/06/2016 00:30    3   6   2016    0   30  
03/06/2016 00:35    3   6   2016    0   35  
03/06/2016 00:40    3   6   2016    0   40  
03/06/2016 00:45    3   6   2016    0   45  
03/06/2016 00:50    3   6   2016    0   50  
03/06/2016 00:55    3   6   2016    0   55  
03/06/2016 01:00    3   6   2016    1   0

使用以下代码:

    program fecha
    implicit none
    integer, dimension(13):: dia, mes, ano, hora, minuto

    character*50 :: formato = '(11x,5x,1x,i1,1x,i1,1x,i4,1x,i1,1x,i2)'

    open (unit = 10, file = 'datos.txt')
    read(10,*)
    read(unit = 10, fmt = formato) dia, mes, ano, hora, minuto

    write(*,*) dia

    close(10)

    end program

为什么这段代码以这种方式读取“dia”:

 3           6        2016           0           0           3           6        2016           0           5           3           6        2016

(我知道它的阅读方式,但不知道为什么)

【问题讨论】:

    标签: fortran format write txt


    【解决方案1】:

    您需要在开头跳过两行并逐行读取值。

    下面的例子是对你的程序稍加修改,运行流畅。

    program fecha
      implicit none
    
      integer               :: i, iounit
      integer, parameter    :: n = 13
      integer, dimension(n) :: dia, mes, ano, hora, minuto
    
      open (newunit = iounit, file = 'datos.txt')
      read (iounit, *)
      read (iounit, *)
    
      do i = 1, n
        read (unit = iounit, fmt = '(16x, i5, i4, i7, 2i5)') dia(i), mes(i), ano(i), hora(i), minuto(i)
        print *, dia(i), mes(i), ano(i), hora(i), minuto(i)
      end do
    
      close (iounit)
    end program
    

    我的输出是

    $ gfortran -g3 -Wall -fcheck=all a.f90 && ./a.out
               3           6        2016           0           0
               3           6        2016           0           5
               3           6        2016           0          10
               3           6        2016           0          15
               3           6        2016           0          20
               3           6        2016           0          25
               3           6        2016           0          30
               3           6        2016           0          35
               3           6        2016           0          40
               3           6        2016           0          45
               3           6        2016           0          50
               3           6        2016           0          55
               3           6        2016           1           0
    

    【讨论】:

    • 非常感谢您的详细回复,这将非常有帮助。
    猜你喜欢
    • 2014-06-26
    • 1970-01-01
    • 2012-02-08
    • 1970-01-01
    • 1970-01-01
    • 2023-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多