简体   繁体   English

在fortran中读取可变长度的实数值

[英]Reading variable length real values in fortran

I have a file with the following text: 我有一个带有以下文本的文件:

-1.065211      246.0638 xlo xhi
-0.615       245.385 ylo yhi
-10 10 zlo zhi

I want to read the numerical values on the lines into a 3*2 real matrix. 我想将线上的数值读入3 * 2的实数矩阵中。
Is it possible to read only two inputs of a record and go to the next line? 是否可以只读取一条记录的两个输入并转到下一行? Something like: 就像是:
read(1,'(2F?.?/)') (matrix(i,1:2),i=1,3)
I've put question marks in F?.? 我在F中加了问号? because the length of my numbers are variable. 因为我的数字长度是可变的。 In other words, I need to read only two items of the record in free-format. 换句话说,我只需要阅读自由格式的记录中的两项。 I understand that you can do this by a loop. 我了解您可以循环执行此操作。 I'm wondering if an elegent one-liner is possible. 我想知道一个优雅的一线客是否可行。

You can achieve what you want with 您可以用实现的目标

do i = 1, 3
   read(fd,*) matrix(i,:)
end do

where it is assumed that fd is connected to the file containing your data. 假定fd已连接到包含您的数据的文件。

The code in @Steve's answer is the simplest one that can accomplish what you want. @Steve答案中的代码是可以完成您想要的最简单的代码。 If you want it in one line , you can simply 如果您希望将其放在一行中 ,则只需

do i=1,3 ; read(fd,*) matrix(i,:) ; end do

If what you are looking for is a way to "read only two inputs of a record and go to the next line" with only one statement , that is, to my knowledge, not possible: Since your real values have different formats, you need to use list-directed input (an asterisk in the format) in order to read multiple lines with the same statement (all other formats for reals require a positive width for input, and since your reals have different widths any specification of a width would result in a runtime error). 如果您要寻找的是仅用一条语句 读取一条记录的两个输入并转到下一行”的方法,就我所知,这是不可能的:由于您的实际值具有不同的格式,因此您需要使用列表导向的输入(格式中的星号)以读取具有相同语句的多行(所有其他格式的实数都需要输入正宽度,并且由于您的实数具有不同的宽度,因此会产生任何宽度规范)在运行时错误中)。

If you want to only use one statement, you will need to explicitly tell the program to read the two strings at the end of each line, even if they are discarded (otherwise the program will try to run a string into a real number, and this will result in a runtime error): 如果您只想使用一条语句,则需要明确告知程序读取每行末尾的两个字符串,即使它们已被丢弃(否则程序将尝试将字符串转换为实数,并且这将导致运行时错误):

character(len=10) :: r
read(1,*) (a(i,:),r,r,i=1,3)

This is less general than @Steve's code (requires that you have exactly 2 character strings per line), and actually requires the additional declaration of the character variable. 它不如@Steve的代码通用(要求每行恰好有2个字符串),并且实际上需要字符变量的附加声明。

Finally, note that you are not reading the array in element sequence order: in Fortran, the order of the elements of the array in the memory is a(1,1) , a(2,1) , a(3,1) , a(1,2) , etc. If the actual arrays you are planning on manipulating are much bigger, the order of the indices may dramatically affect performance, since your consecutive memory accesses may address distant parts of the memory rather than contiguous parts of it. 最后,请注意,您不是按元素顺序读取数组:在Fortran中,数组在内存中的元素顺序为a(1,1)a(2,1)a(3,1)a(1,2)等。如果您计划操作的实际数组大得多,则索引的顺序可能会极大地影响性能,因为连续的内存访问可能会寻址内存的远处而不是连续的部分它。 When you are writing a program in Fortran, as in any other language (which may use a different ordering), you need to give some thought to the order of the indices of arrays. 在用Fortran编写程序时,就像使用其他任何语言(可能使用不同的顺序)一样,您需要考虑数组索引的顺序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM