从fortran的txt文件中读取数据

She*_*nny 6 format fortran

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

1234567890123456 123456.789 987654.321 673647.890 654356.890
6172876534567890 768909.098 234543.890 654321.908 987890.090
Run Code Online (Sandbox Code Playgroud)

我有以下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)
Run Code Online (Sandbox Code Playgroud)

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

1234567890123456*********89987.656    0.322
6172876534567890*********98234.547    0.891
Run Code Online (Sandbox Code Playgroud)

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

非常感谢

M. *_* B. 9

列表导向的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
Run Code Online (Sandbox Code Playgroud)


小智 5

我有过最艰难的时间尝试使用 read,但最后......如果你想读取存储在 .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
Run Code Online (Sandbox Code Playgroud)

例如,“array.txt”文件必须是这样的(并放置在主目录的同一文件夹中):

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
Run Code Online (Sandbox Code Playgroud)

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


pro*_*ils 5

对 @ Andr\xc3\xa9s Arg\xc3\xbcello Guill\xc3\xa9n答案稍作修改。

\n\n

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

\n\n
CHARACTER(128) :: buffer\n\ninteger strlen, rows, cols\nreal, dimension(:,:), allocatable :: x\n\nOPEN (1, file = 'matrix.txt', status='old', action='read')\n\n!Count the number of columns\n\nread(1,'(a)') buffer !read first line WITH SPACES INCLUDED\nREWIND(1) !Get back to the file beginning\n\nstrlen = len(buffer) !Find the REAL length of a string read\ndo while (buffer(strlen:strlen) == ' ') \n  strlen = strlen - 1 \nenddo\n\ncols=0 !Count the number of spaces in the first line\ndo i=0,strlen\n  if (buffer(i:i) == ' ') then\n    cols=cols+1\n  endif\nenddo\n\ncols = cols+1\n\n!Count the number of rows\n\nrows = 0 !Count the number of lines in a file\nDO\n  READ(1,*,iostat=io)\n  IF (io/=0) EXIT\n  rows = rows + 1\nEND DO\n\nREWIND(1)\n\nprint*, 'Number of rows:', rows\nprint*, 'Number of columns:', cols\n\nallocate(x(rows,cols))\n\ndo I=1,rows,1\n  read(1,*) x(I,:)\n  write(*,*) x(I,:)\nenddo\n\nCLOSE (1)\n
Run Code Online (Sandbox Code Playgroud)\n\n

矩阵.txt

\n\n
0.0 1.0 2.0\n3.0 4.0 5.0\n6.0 7.0 8.0\n
Run Code Online (Sandbox Code Playgroud)\n