问题描述
我正在写一个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
你能帮我解决这个问题吗?
非常感谢
)更容易,特别是在输入。尽管如此,有时候可以使用完整的IO控制,因此值得理解。输入时,数据项和描述符必须按列排列。对于输入,在Fw.d中,如果数据项中有一个小数点,则d无关紧要。输入和输出上的字段必须足够宽。需要有足够的描述符,其中包含与变量和数据项匹配的类型。比较这个例子程序:
program test_read
pre>
implicit none
整数,参数: :VLI_K = selected_int_kind(18)
整数,参数:: DR_K = selected_real_kind(14)
整数(VLI_K)::
real(DR_K):: a,b ,c,d
open(unit = 15,file =data.txt,status ='old',&
access ='sequential',form ='formatted', (15,110)i,a,b,c,d
110格式(I16,4(1X,F10.0))
(*,120)i,a,b,c,d
120格式(I18,4(2X,F12.3))
read(15,*) a,b,c,d
写(*,120)i,a,b,c,d
结束程序test_read
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
解决方案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
这篇关于从fortran的txt文件中读取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!