![](/img/trans.png)
[英]How to undo reading lines from text file in Fortran/ How to Re-read the same lines already read before in Fortran
[英]Read data from updating dat file in Fortran
我有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
然后將此數據寫入輸出文件。
以下代碼完成了您所要求的。 此代碼假定數據存儲在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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.