reading data from txt file in fortran

前端 未结 6 1306
渐次进展
渐次进展 2020-12-17 23:53

I am writing a FORTRAN program that reads data from a text file and writing it to the console. the data file looks something like this

1234567890123456 12345         


        
6条回答
  •  时光说笑
    2020-12-17 23:56

    I had the hardest time ever trying to use read, but finally... If you want to read a matrix stored in a .txt file use this:

    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
    

    And the "array.txt" file must be like this for instance (And placed in the same folder of the 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
    

    Hope it works for everyone out there

提交回复
热议问题