0

我有一个 dat 格式的代码输出。该文件具有以下格式

Text
Text
Text
Text
3241234234
234234
23423423
34123424
1324234
iteration pressure temperature density
1 1234 312 2.12
2 1235 321 2.15
3 1234 312 2.12
4 1235 321 2.15
5 1234 312 2.12
6 1235 321 2.15
pressure temperature density
7 1234 312 2.12
8 1235 321 2.15
9 1234 312 2.12
10 1235 321 2.15
11 1234 312 2.12
warning pressure update is not linked
12 1235 321 2.15
pressure temperature density
13 1234 312 2.12
14 1235 321 2.15
15 1234 312 2.12
warning pressure update is not linked
16 1235 321 2.15
17 1234 312 2.12
18 1235 321 2.15
end of iterations
simulation time
end loop
end of code

我编写了一个代码,在其中打开 dat 文件。使用 iostat 将其作为文本阅读。然后我跳过标题文本行和随机数等直到迭代行。然后读取数字(迭代压力温度密度)。但是我被困在需要帮助的几个地方。

  1. 开头的文本行不是恒定的。有时这些是 4 行,有时是 5 或 6 行。每次我都必须调整行数并重新编译。有没有办法自动化这个。我的意思是代码本身会计算文本行并跳过它们。下一个随机数也是如此。

  2. 我想要做的是从开始到迭代跳过这些行。但这些也在发生变化。仅读取数字数据

    1 1234 312 2.12 2 1235 321 2.15 3 1234 312 2.12 4 1235 321 2.15 5 1234 312 2.12 6 1235 321 2.15 7 1234 312 2.12 8 1235 321 2.15 9 1234 312 2.12 10 1235 321 2.15 11 1234 312 2.12 12 1235 321 2.15 13 1234 312 2.12 14 1235 321 2.15 15 1234 312 2.12 16 1235 321 2.15 17 1234 312 2.12 18 1235 321 2.15

然后将此数据写入输出文件。

4

1 回答 1

0

以下代码可以满足您的要求。此代码假定数据存储在input.dat中,并将输出打印到output.dat中。逻辑很简单。首先,读取行,直到检测到以关键字 WORD_OF_INTEREST(此处为迭代)开头的行。然后我们开始读取剩余的行,假设每行有 4 个字段(即迭代、压力温度密度)。不遵循此模式的行将被跳过。

评论将帮助您理解算法。

    program readData

       implicit none

       integer, parameter :: wp = selected_real_kind(15) ! double-precision floats
       integer, parameter :: ip = selected_int_kind(8) ! long integers

       character (len = *), parameter ::               &
          WORD_OF_INTEREST = 'iteration'      

       integer (kind = ip), parameter ::               & 
          LENGTH_WORD = len(WORD_OF_INTEREST),         &! calculate length of the word you are trying to find; that is, "iteration"
          NO_READING_ERROR = 0,                        &
          END_OF_FILE = -1

       integer (kind = ip) ::                          &
          handleFileInput,                             &
          handleFileOutput,                            &
          iteration,                                   &
          idError

       real (kind = wp) ::                             &
          pressure,                                    &
          temperature,                                 &
          density

       character (len = LENGTH_WORD) ::                & 
          line

    ! --------------------------------------------------------------------------------------------------
    ! --------------------------------------------------------------------------------------------------
    ! --------------------------------------------------------------------------------------------------
    ! Start executable section 
    ! --------------------------------------------------------------------------------------------------
    ! --------------------------------------------------------------------------------------------------
    ! --------------------------------------------------------------------------------------------------

       ! Open the input and output files
       open(newUnit = handleFileInput, file = 'input.dat')
       open(newUnit = handleFileOutput, file = 'output.dat')

       ! Read data file until a line starting with WORD_OF_INTEREST is encountered
       do

          read(handleFileInput, *) line
          if (line == WORD_OF_INTEREST) exit

       end do


       ! Read the rest of the file
       do

          read(handleFileInput, *, iostat = idError) iteration, pressure, temperature, density

          ! Handle different types of errors
          if (idError == NO_READING_ERROR) then

             write(handleFileOutput, *) iteration, pressure, temperature, density

          else if (idError == END_OF_FILE) then

             exit

          else

             continue 

          end if

       end do



       close(handleFileInput)
       close(handleFileOutput)

    end program readData
于 2019-02-19T18:40:09.147 回答