Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
603 views
in Technique[技术] by (71.8m points)

format - reading data from txt file in fortran

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 123456.789 987654.321 673647.890 654356.890
6172876534567890 768909.098 234543.890 654321.908 987890.090

I have the following lines of FORTRAN code that reads data and just writes them to the console

 OPEN(1,FILE='data.txt')
    READ(1,'(I16,3F9.3)') A ,B, C, D
    WRITE (*, '(I16,3F9.3)') A,B,C,D
   CLOSE(1)

Instead of getting displayed as the same values in the text file, the following is the output

1234567890123456*********89987.656    0.322
6172876534567890*********98234.547    0.891

Can you please help me with this.

Thanks much

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...