从fortran的txt文件中读取数据

时间:2012-01-11 23:47:42

标签: format fortran

我正在编写一个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

你能帮我解决这个问题。

非常感谢

6 个答案:

答案 0 :(得分:8)

列表导向的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

答案 1 :(得分:3)

我有过最艰难的时间尝试使用阅读,但最后...... 如果要读取存储在.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

希望它适用于那里的所有人

答案 2 :(得分:2)

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

答案 3 :(得分:1)

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

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

答案 4 :(得分:1)

轻微修改@ AndrésArgüelloGuillé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)

<强> matrix.txt

0.0 1.0 2.0
3.0 4.0 5.0
6.0 7.0 8.0

答案 5 :(得分:0)

通常以非固定格式读取数据会更好。并留下一些前导空格,以便在写出数字时适合数字。

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
相关问题