Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
File io Fortran:如何高效地跳过多行数据文件_File Io_Fortran - Fatal编程技术网

File io Fortran:如何高效地跳过多行数据文件

File io Fortran:如何高效地跳过多行数据文件,file-io,fortran,File Io,Fortran,我有一个格式化的数据文件,通常有几十亿行长,有几行可变长度的标题。数据文件的格式如下: # header 1 # header 2 # headers are of variable length. # data begins from next line. 1.23 4.56 7.89 0.12 2.34 5.67 8.90 1.23 : : # billions of lines of data, each row

我有一个格式化的数据文件,通常有几十亿行长,有几行可变长度的标题。数据文件的格式如下: # header 1 # header 2 # headers are of variable length. # data begins from next line. 1.23 4.56 7.89 0.12 2.34 5.67 8.90 1.23 : : # billions of lines of data, each row the same length, same format. -- end of file --

我想从这个文件中提取一部分数据,我当前的代码如下所示:

<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>
问题是,Nj可能在10亿到几十亿之间,所以运行代码需要一些时间


我的问题是,有没有更有效的方法跳过数百万行数据?在坚持使用Fortran的同时,我能想到的唯一方法是使用直接访问打开文件,并在打开文件时跳转到所需的行。

正如您所建议的,直接访问似乎是最好的选择。但这要求所有记录都具有相同的长度,这是您的头所违反的。另外,为什么使用格式化输出?对于这样一个长度的文件,很难想象有人在读这个文件。如果使用未格式化IO,文件将更小,IO速度更快。可能会创建两个文件,一个文件的标题元数据为人类阅读器格式,另一个文件的数据为本机格式。本机/二进制表示意味着可移植性的丧失,如果要将文件移到不同的计算机体系结构或使它们可用几十年,这是一个值得考虑的问题。否则它可能是值得的便利。其他选择是使用更复杂的文件格式,将元数据和数据结合在一起,如HDF5或FITS,但对于一个人的两个程序之间的通信,这可能太过分了

您也可以以流的形式打开,读取标题,然后计算行首的位置。文件是怎么写的?在记录分隔符/换行符方面需要处理哪些约定?该文件是否可以被视为一个无格式的字符数据流,并使用ACHAR10作为行尾?该文件是我的另一段代码的输出。这些数字是整数和双精度数,它们被写入文件时使用了如下内容:writeunit=unit,fmt='I5,I4,ES12.4,ES16.8',循环次数与数据片段的循环次数相同。感谢您的建议。文件格式化是我第一次开发主代码时遗留下来的一点,输出数据要小得多。所以现在可能是考虑把它变成二进制输出的时候了。