从fortran的txt文件中读取数据 [英] reading data from txt file in fortran

查看:1558
本文介绍了从fortran的txt文件中读取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个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

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

非常感谢

解决方案


  program test_read 

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
pre>

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文件中读取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆