【发布时间】:2015-06-15 12:13:58
【问题描述】:
我有一个格式化的数据文件,它通常有数十亿行长,有几行可变长度的标题。数据文件采用以下形式:
# 标题 1 # 标题 2 # 标头长度可变。 # 数据从下一行开始。 1.23 4.56 7.89 0.12 2.34 5.67 8.90 1.23 : : # 数十亿行数据,每行长度相同,格式相同。 -- 文件结束 --我想从这个文件中提取一部分数据,我当前的代码如下:
<pre>
do j=1,jmax !Suppose I want to extract jmax lines of data from the file.
[algorithm to determine number of lines to skip, "N(j)"]
!This determines the number of lines to skip from the previous file
!position, when the data was read on j-1th iteration.
!Skip N-1 lines to go to the next data line to read off:
do i=1,N-1
read(unit=unit,fmt='(A)')
end do
!Now read off the line of data I want:
read(unit=unit,fmt='(data_format)'),data1,data2,etc.
!Data is stored in some arrays.
end do
</pre>
问题是,N(j) 可以在 1 到几十亿之间,因此运行代码需要一些时间。
我的问题是,有没有更有效的方法来跳过数百万行数据?在坚持使用 Fortran 的同时,我能想到的唯一方法是通过直接访问打开文件并在打开文件时跳转到所需的行。
【问题讨论】:
-
您也可以作为流打开,读取标题,然后计算行首的位置。
-
文件写了什么?在记录分隔符/换行符方面需要处理哪些约定?可以将文件视为带有 ACHAR(10) 用于行尾的未格式化字符数据流吗?
-
该文件是我的另一个代码的输出。这些数字是整数和双精度,它们被写入文件中,例如:“write(unit=unit,fmt='(I5,I4,ES12.4,ES16.8)'))”,循环为有很多数据。