reading data from txt file in fortran

前端 未结 6 1308
渐次进展
渐次进展 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-18 00:12

    List-directed IO (i.e., *) is easier, especially on input. Nevertheless, there are times to use full IO control so that is worth understanding. On input, the data items and descriptors must line up by column. For input, in Fw.d, the d doesn't matter if you have a decimal point in the data item. The fields must be wide enough on both input and output. There need to be enough descriptors, of types which match the variables and the data items. Compare to this example program:

    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
    

提交回复
热议问题