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

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

问题描述

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

我有以下几行 FORTRAN 代码,用于读取数据并将它们写入控制台

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.

非常感谢

推荐答案

List-directed IO(即*)更容易,尤其是在输入方面.不过,有时会使用完整的 IO 控制,因此值得理解.在输入时,数据项和描述符必须按列排列.对于输入,在 Fw.d 中,如果数据项中有小数点,d 无关紧要.输入和输出的字段必须足够宽.需要有足够的描述符,类型与变量和数据项匹配.与此示例程序比较:

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天全站免登陆