【问题标题】:reading data from txt file in fortran用fortran从txt文件中读取数据
【发布时间】:2012-02-08 08:18:51
【问题描述】:

我正在编写一个 FORTRAN 程序,它从文本文件中读取数据并将其写入控制台。数据文件看起来像这样

1234567890123456 123456.789 987654.321 673647.890 654356.890
6172876534567890 768909.098 234543.890 654321.908 987890.090

我有以下几行 FORTRAN 代码,用于读取数据并将它们写入控制台

 OPEN(1,FILE='data.txt')
    READ(1,'(I16,3F9.3)') A ,B, C, D
    WRITE (*, '(I16,3F9.3)') A,B,C,D
   CLOSE(1)

不是在文本文件中显示为相同的值,而是以下输出

1234567890123456*********89987.656    0.322
6172876534567890*********98234.547    0.891

你能帮我解决这个问题吗?

非常感谢

【问题讨论】:

    标签: format fortran


    【解决方案1】:

    列表导向的 IO(即 *)更容易,尤其是在输入时。不过,有时会使用完整的 IO 控制,因此值得理解。在输入时,数据项和描述符必须按列排列。对于输入,在 Fw.d 中,如果数据项中有小数点,d 无关紧要。输入和输出的字段必须足够宽。需要有足够的描述符,类型与变量和数据项匹配。与此示例程序比较:

    program test_read
    
       implicit none
       integer, parameter :: VLI_K = selected_int_kind (18)
       integer, parameter :: DR_K = selected_real_kind (14)
    
       integer (VLI_K) :: i
       real (DR_K) :: a, b, c, d
    
       open (unit=15, file="data.txt", status='old',    &
                 access='sequential', form='formatted', action='read' )
    
       read (15, 110)  i, a, b, c, d
       110 format (I16, 4(1X, F10.0) )
       write (*, 120) i, a, b, c, d
       120 format ( I18, 4 (2X, F12.3) )
    
       read (15, *) i, a, b, c, d
       write (*, 120) i, a, b, c, d
    
    end program test_read
    

    【讨论】:

      【解决方案2】:

      我经历了有史以来最艰难的一次尝试使用阅读,但最后...... 如果您想读取存储在 .txt 文件中的矩阵,请使用以下命令:

      program FILEREADER
      
         real, dimension(:,:), allocatable :: x
         integer :: n,m
      
         open (unit=99, file='array.txt', status='old', action='read')
         read(99, *), n
         read(99, *), m
         allocate(x(n,m))
      
         do I=1,n,1
            read(99,*) x(I,:)
            write(*,*) x(I,:)
         enddo
      
      end
      

      并且“array.txt”文件必须是这样的(并且放在main的同一个文件夹中):

      4
      3
      0.0 1.0 2.0
      3.0 4.0 5.0
      6.0 7.0 8.0
      9.0 10.0 11.0
      

      希望它适用于那里的每个人

      【讨论】:

      • 答案应该尽可能详细地解释问题和解决方案,以便读者能够理解它们,而不仅仅是使用它们(尤其是在使用它们时需要他们编辑给定的代码)。跨度>
      【解决方案3】:

      对@Andrés Argüello Guillén 答案稍作修改。

      与大多数其他解决方案不同,我的代码不会强制您提前指定行数和列数。

      CHARACTER(128) :: buffer
      
      integer strlen, rows, cols
      real, dimension(:,:), allocatable :: x
      
      OPEN (1, file = 'matrix.txt', status='old', action='read')
      
      !Count the number of columns
      
      read(1,'(a)') buffer !read first line WITH SPACES INCLUDED
      REWIND(1) !Get back to the file beginning
      
      strlen = len(buffer) !Find the REAL length of a string read
      do while (buffer(strlen:strlen) == ' ') 
        strlen = strlen - 1 
      enddo
      
      cols=0 !Count the number of spaces in the first line
      do i=0,strlen
        if (buffer(i:i) == ' ') then
          cols=cols+1
        endif
      enddo
      
      cols = cols+1
      
      !Count the number of rows
      
      rows = 0 !Count the number of lines in a file
      DO
        READ(1,*,iostat=io)
        IF (io/=0) EXIT
        rows = rows + 1
      END DO
      
      REWIND(1)
      
      print*, 'Number of rows:', rows
      print*, 'Number of columns:', cols
      
      allocate(x(rows,cols))
      
      do I=1,rows,1
        read(1,*) x(I,:)
        write(*,*) x(I,:)
      enddo
      
      CLOSE (1)
      

      ma​​trix.txt

      0.0 1.0 2.0
      3.0 4.0 5.0
      6.0 7.0 8.0
      

      【讨论】:

        【解决方案4】:

        我使用固定格式是因为编辑和检查具有固定列结构的输入文件比之字形数据更容易。 我的问题是 Fortran 运行时阅读器程序如何解释小数点的存在和不存在。我不确定我的解决方案是不是最好的,但我将数据行作为字符数组读取,将它们拆分为长度为 12 个字符的字段,然后通过read(*) 语句读取字段。

        【讨论】:

          【解决方案5】:

          通常最好以非固定格式读取数据。并留出一些前导空格,以便在写出数字时可以容纳。

          integer(8) :: i
          real(4) :: x, y, z
          open(unit=1, file='data.txt')
          read(1,*)i, x, y, z
          write(*,'(i16, 3f11.3)')i, x, y, z
          end
          

          【讨论】:

            【解决方案6】:

            原因是您指定的宽度对于实数来说太小了。通常当宽度不合适时,fortran 会显示星号,这在您的情况下会发生。

            您有 9 位数字,但至少需要 10 位,因为逗号也占一列。 所以用 3F10.3 替换 3F9.3 应该可以解决问题。

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2018-11-01
              • 2013-06-23
              • 2023-03-26
              • 1970-01-01
              • 1970-01-01
              • 2021-03-29
              • 1970-01-01
              • 2012-01-13
              相关资源
              最近更新 更多