Fortran 中 READ 的含义

发布于 2024-07-27 03:29:03 字数 93 浏览 5 评论 0原文

READ() 在 Fortran 中做什么?

例如:

READ(1,82)

What does READ() do in Fortran?

For example:

READ(1,82)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

坠似风落 2024-08-03 03:29:03

1 是文件句柄,您必须使用正确的 open 调用来打开它。
82 是引用格式的标签,表示您将如何以可视格式报告数据。

        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 位数字

如果我现在运行程序,并且不完全遵循此格式,则程序将抱怨并崩溃,因为前 4 列预计表示一个整数(由于 I4 格式)和“5 3”。 不是有效的整数

$ ./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 之前的三个空格)将执行正确的操作(有一点容差,并不严格)

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

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
$ 
不如归去 2024-08-03 03:29:03

根据标签 82 处的 FORMAT 语句,它从 1 号“单元”(打开的文件)读取。但是,由于该语句没有列出任何变量,因此它没有地方放置它正在读取的数据,这不太可能有帮助; READ(1,82) FOOBAR 将更有用地将其正在读取的数据放入变量 FOOBAR 中。

It reads from "unit" (opened file) number 1, according to the FORMAT statement at label 82. However since the statement doesn't list any variables it has no place to put the data it's reading, which is unlikely to help; READ(1,82) FOOBAR would more usefully put the data it's reading in variable FOOBAR.

離殇 2024-08-03 03:29:03

您可以使用 fortran“read”语句执行更多操作。

考虑:
read (unit #, format, options) ... generic

read (7,*,end=10)

其中,“7”是读取的单元号,“*”是格式(本例中默认),“10”是程序控制的行号跳转到读取设备/文件到达 eof 时。 “选项”槽可以填充诸如“err ='要转到的行号'”或iostat,advance =“no”之类的内容。 您可以查看 更多内容

格式部分位于何处您可以更精确地指定您期望的数据格式。 例如,如果您有如下格式说明符:

read (25,"(2X, 2I5, F7.3, A)")

这里,“2X”表示 2 个空格,“2I5”表示 2 个 5 位整数,“F7.3”表示具有以下性质的十进制值:总长度为7,小数点后三位。 “A”指的是一个字符。 您可以查看更多这些

干杯!

You can do a few more things with the fortran "read" statement.

consider:
read (unit #, format, options) ... generic

read (7,*,end=10)

Where, "7" is the unit number read from, "*" is the format (default in this case), and "10" is the line number that the program control jumps to when the read device / file reaches the eof. The "options" slot can be filled with such things as "err='line number to go to'", or iostat, advance="no". You can check out some more of these

The format part is where you can specify more precisely the format of the data that you expect. For instance, if you have a format specifier like:

read (25,"(2X, 2I5, F7.3, A)")

Here, the "2X", refers to 2 spaces, the "2I5", refers to 2 integers that are 5 digits, "F7.3", refers to a decimal value which has a total length of 7, with three digits after the decimal. The "A" refers to a character. You can check out some more of these

CHEERS!

慕巷 2024-08-03 03:29:03

“1”是用于在 Fortran 中打开文件的单位,“82”指定读取命令的格式。

open(1,file=fname,status='unknown')
read(1,82) var_name
82 format(2I5)

上面的代码打开一个名为“fname”的文件,读取命令从文件 fname 中读取,因为它是用单位“1”打开的,并且读取命令以格式 82 指定的格式读取。有关 Fortran 格式的详细信息如下:

nim (Integer Specification)
nfm.d (Floating point Specification)
nEm.d(Exponential Specification)
nAm (string specification)

where
"m" is the number of character spaces reserved for printing. (should be more than what you are reading otherwise read statement would not give correct results)
"n" is the number of integers, floating point, characters or exponential numbers that you want to read.
"d" are the number of decimal places up to which you want to read.

"1" the unit that you used to open a file in fortran and "82" specifies the format for the read command.

open(1,file=fname,status='unknown')
read(1,82) var_name
82 format(2I5)

The code above opens a file called "fname" the read command reads from the file fname as it was opened with a unit "1" and the read command reads in the format as specified by format 82. Details on formatting in fortran is given below:

nim (Integer Specification)
nfm.d (Floating point Specification)
nEm.d(Exponential Specification)
nAm (string specification)

where
"m" is the number of character spaces reserved for printing. (should be more than what you are reading otherwise read statement would not give correct results)
"n" is the number of integers, floating point, characters or exponential numbers that you want to read.
"d" are the number of decimal places up to which you want to read.
温柔少女心 2024-08-03 03:29:03

它使用编号 82 的语句指定的格式从单元 1 读取。

It reads from unit 1 using the format specified by the statement numbered 82.

傲娇萝莉攻 2024-08-03 03:29:03

当 Fortran 读取文件时,它要求 READ 语句唯一标识该文件。 使用 Fortran 单元标识符来完成识别。

单元标识符可以是以下之一:

1) 值大于或等于 0 的整型变量或表达式。

2) 仅在 READ 和 WRITE 语句中允许使用星号 (*)。 在 READ 语句中,星号表示单元 100(标准输入)。

单元编号最好使用 newunit 提供

open(newunit=i,file='test')

在使用任何单元编号之前,使用 INQUIRE 语句检查其有效性(存在性),如下例所示:

logical :: idok, fop 
inquire (unit=i, exist=idok, opened=fop)
if (idok .and. .not. fop) then
  open (unit = i, ...)
endif

然后我们有 FORMAT 语句,这是一个带标签的语句,可以出现在格式可见的程序。

它的形式为

READ(*,100) I, J, K

FORMAT 语句

100 FORMAT(I10,I10,I10)

一个稍有不同的 FORMAT 语句是,

100 FORMAT(3I10.8)

它再次生成三个宽度为 10 的右对齐 INTEGER,但这次需要至少打印 8 位数字。

When Fortran reads from a file, it required that the READ statements uniquely identify the file. The identification is done using the Fortran unit identifier.

A unit identifier can be one of the following:

1) An integer variable or expression whose value is greater than or equal to 0.

2) An asterisk (*) is allowed only on READ and WRITE statements. On READ statements, an asterisk refers to unit 100 (standard input).

Unit numbers are best supplied using newunit

open(newunit=i,file='test')

Use the INQUIRE statement to check the validity (existence) of any unit number prior to using it, as in the following example:

logical :: idok, fop 
inquire (unit=i, exist=idok, opened=fop)
if (idok .and. .not. fop) then
  open (unit = i, ...)
endif

Then we have the FORMAT statement, a labelled statement which can appear in any portion of the program in which the format is visible.

It is of the form

READ(*,100) I, J, K

The FORMAT statement

100 FORMAT(I10,I10,I10)

A slightly different FORMAT statement is

100 FORMAT(3I10.8)

which again yields three right-justified INTEGERs of width 10 but this time requires a minimum of 8 digits to be printed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文