FORTRAN READ() [英] FORTRAN READ()

查看:1627
本文介绍了FORTRAN READ()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



READ()在FORTRAN中做什么?

>

例如:

  READ(1,82)


解决方案

1是文件句柄,您必须打开正确的公开呼叫。
82是一种引用格式的标签,意味着您将如何以视觉格式报告数据。

 程序foo 
隐式无
整数:: i
双精度:: a

写(*,*)'给我一个整数和一个浮点数'
read(*,82)i,a
write(*,82)i,
82格式(I4,F8.3)
结束程序


如果我运行程序现在,我没有完全遵循这种格式,程序会发出抱怨和崩溃,因为前4列预期会表示一个整数(由于I4格式),5 3。是不是一个有效的整数

  $ ./a.out 
给我一个整数和一个浮点数
5 3.5
在文件test.f(Unit 5)的第7行
Traceback:不可用,用-ftrace = frame或-ftrace = full编译
Fortran运行时错误:整数阅读

然而,正确的规格(请注意数字5之前的三个空格)将执行正确操作(有一点宽容,这不是严格的)

  $ ./a.out 
给我一个整数和一个浮点数
5 3.5
5 3.500
$


Excuse me if this is a duplicate question, but I didn't see anything exactly like this.

What does READ() do in FORTRAN?

For example:

READ(1,82)

解决方案

1 is the file handle, which you have to open with the proper open call. 82 is a label that references a format, meaning how you will report the data in terms of visual formatting.

        program foo
        implicit none
        integer :: i
        double precision :: a

        write (*,*) 'give me an integer and a float'
        read (*,82) i,a
        write (*,82) i,a
82      format (I4, F8.3)
        end program

In this example, the program accepts from the standard input (whose unit number is not specified, and so I put a *) an integer and a floating point value. the format says that the integer occupies the first four columns, then I have a float which stays in 8 columns, with 3 digits after the decimal point

If I run the program now, and I don't follow exactly this format, the program will complain and crash, because the first 4 columns are expected to represent an integer (due to the I4 format), and "5 3." is not a valid integer

$ ./a.out 
 give me an integer and a float
5 3.5
At line 7 of file test.f (Unit 5)
Traceback: not available, compile with -ftrace=frame or -ftrace=full
Fortran runtime error: Bad value during integer read

However, a correct specification (please note the three spaces before the number 5) will perform the correct operation (with a little tolerance, it's not that strict)

$ ./a.out 
 give me an integer and a float
   5 3.5
   5   3.500
$ 

这篇关于FORTRAN READ()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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