我有一个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将其阅读为文本。然后我跳过标题文本行和随机数等,直到迭代行。然后读取数字(迭代压力温度密度)。但是我被困在需要帮助的地方。
开头的文本行不是恒定的。有时这些是4行,有时是5或6行。每次我必须调整行数并再次编译时。有没有办法使它自动化。我的意思是代码本身将计算文本行并跳过它们。下一个随机数相同。
我想要做的是从开始到迭代都跳过这些行。但是这些也在改变。仅读取数字数据
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
,然后将此数据写入输出文件。
答案 0 :(得分: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