2012-01-11 223 views
3

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

123456789.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) 

非但没有显示为文本的值相同的文件,以下是输出

123456789*********89987.656 0.322 
6172876534567890*********98234.547 0.891 

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

感谢很多

回答

7

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

我使用固定的格式,因为编辑和具有固定的列结构的输入文件的检查比Z字形数据的更容易。 我的问题是Fortran运行时读取程序如何解释小数点的存在与否。我不确定我的解决方案是否是最好的,但是我将数据行作为字符数组读取,将它们分割为长度为12个字符的字段,然后通过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 

与“阵列。 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 

希望工程给大家在那里

+0

答案应该尽量解释,读者可以了解他们,如果需要的问题和足够详细的解决方案,而不是仅仅使用它们(尤其是当使用它们需要他们编辑给定的代码)。 – 2014-06-17 11:08:17

0

的原因是,你specifyin g实际数字的宽度太小。通常当宽度不适合时,fortran会显示星号,这发生在您的案例中。

您有9位数字,但您至少需要10位数字,因为逗号也会占据一列。因此用3F10.3代替3F9.3应该能够解决这个问题。

0

稍作修改@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