1

我在 FORTRAN 中有 2 个问题(我是这种语言的新手)。

我有以下代码行:

  OPEN(UNIT=79, FILE='sampling.txt', FORM='FORMATTED')
  READ(79,*) NP1,NP2,IW 

NP1,NP2,IW 被声明为整数。

打开的“格式化”是什么?我在读什么数据?READ 行是否读取单行并将数据放在 NP1、NP2、IW 中?数据是逗号分隔的吗?空间分隔?

我的下一个问题是:

D_IN = (RD/1000000)**(2./3.)/9.81**(1./3.) 

这条线是做什么的??(D_IN 和 RD 是 REAL*8)有人可以把它翻译成 C 吗?

4

2 回答 2

3

"Formatted" basically means text output - i.e. human readable. The alternative is "unformatted", which allows the processor to write the file using bits and bytes (or whatever its equivalent is) rather than text. In C, the distinction is made at the library call level - fprintf (~formatted) versus fwrite (~unformatted).

Your read statement uses what is known as list directed formatting (nominated by the * in the second position of the parenthesised list) - the format of the input is determined based on the list of items in the input, rather than being explicitly specified by the programmer. The language has a set of rules around how input is translated under list directed formatting - rules that made a lot of sense back in the day of punched cards, but may surprise users of today. As a brutally incomplete summary - records (which may be multiple lines) will be read from the file until three values have been read Those values will be interpreted as integers. Values within a record may be separated by commas or blanks.

Assuming the bold formatting was inadvertent (if not, the line is a syntax error, the Fortran 90 processor will give you a diagnostic, the number of ways in which to get a syntax error in the C language is rather large) the line starting with D_IN is an assignment statement. The value of the expression on the right of the = will be assigned to the variable on the left. The equivalent C is almost a literal transcription (you would typically need to append f after the floating point constants to use the analogous numeric type, but this depends on the specifics of your Fortran processor and C implementation).

于 2012-09-24T22:58:36.210 回答
0

A formatted file is more or less a text file. Formatted means that numbers and other variables are stored as characters. Otherwise the actual formatting of the file is up to you. In your example the * in the read statement means a so called "list directed format" which lets a lot of freedom for the processor when writing and reading. If you need more, the formatted I/O is one of the more difficult Fortran aspects and you should study on of the numerous resources on the web.


The other question: The operator ** is the power operator. If the second operand is not a small integer use the function pow() in C when translating.

于 2012-09-24T22:57:46.680 回答