READ在Fortran中的含义 [英] Meaning of READ in Fortran

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

问题描述

READ()在Fortran中做什么?

What does READ() do in Fortran?

例如:

READ(1,82)

推荐答案

1是文件句柄,您必须使用适当的open调用来打开它. 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

在此示例中,程序从标准输入(未指定单位编号,因此我输入*)接受整数和浮点值.格式显示整数占据了前四列,然后我有一个浮点数,该浮点数保留在8列中,小数点后有3位数字

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

如果我现在运行该程序,但没有完全遵循这种格式,则该程序将报错并崩溃,因为前四列应表示一个整数(由于I4格式),而"5 3 ."不是有效的整数

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

但是,正确的规范(请注意数字5之前的三个空格)将执行正确的操作(稍有容忍,这并不是那么严格)

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
$ 

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

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