【问题标题】:How to undo reading lines from text file in Fortran/ How to Re-read the same lines already read before in Fortran如何在 Fortran 中撤消从文本文件中读取的行/如何在 Fortran 中重新读取之前已经读取的相同行
【发布时间】:2021-02-23 21:38:22
【问题描述】:

文本文件中有一组数据,即每小时的温度和降雨量。我想在 Fortran 中以特定方式读取这些数据。

文本文件中的数据:

Day 1 Hour 1  Temp rain
Day 1 Hour 2  Temp rain
Day 1 Hour 3  Temp rain
...
Day 1 Hour 24 Temp rain
Day 2 Hour 1  Temp rain
Day 2 Hour 2  Temp rain
Day 2 Hour 3  Temp rain
...
Day 2 Hour 24  Temp rain
Day 3 Hour 1   Temp rain
...

.

DO loop1
    Read the first 24 hours of data from the text file
    {Do some procedures using First 24 hour data}
    Read Second 24 hours of data from the text file
    {Do some procedures using First 24 hour data}
End DO

我希望下一个 DO 循环按以下方式工作

DO loop2
    Read second 24 hours  **(I want to read the Second 24-hour data 
    again in this loop, how can I read this set again since its once read in loop 1?.** 
    {Do some procedures using second 24-hour data}
    Read the third 24 hours of data from the text file
    {Do some procedures using third 24-hour data}
End DO loop2

【问题讨论】:

  • 同意@IanBush 的问题需要改进。也就是说,执行此操作的正常方法是将数据保存在缓存中以备后用。类似于将数据文件读入数组。

标签: text fortran line undo


【解决方案1】:

您的问题令人困惑。

我的理解是,您需要将第一天与第二天进行比较,然后将第二天与第三天进行比较,然后将第三天与第四天进行比较,以此类推。

出于某种原因,您认为每次都需要读取数据。事实并非如此。您可以一次读取所有数据,将所有数据保存在内存中,或者,如果它太大(而且它需要非常大才能太大,别忘了,10MB 的内存可以容纳超过 10百万个 64 位浮点数),您可以像这样每天读取它:

real :: data1(24), data2(24)
...
<read data1>
<work on data1>
do
    <read data2>
    <if EOF exit>
    <work on data2>
    <compare data1 and data2>
    data1 = data2
end do

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-22
    • 2020-04-07
    • 2014-06-26
    • 2020-11-18
    相关资源
    最近更新 更多