Следующий код делает то, что вы просили.Этот код предполагает, что данные хранятся в input.dat и распечатает вывод в output.dat .Логика довольно проста.Сначала строки читаются до тех пор, пока не будет обнаружена строка, начинающаяся с ключевого слова WORD_OF_INTEREST (здесь iteration ).Затем мы начинаем читать оставшиеся строки, предполагая, что в каждой строке 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