Internal write and read in Fortran
write can target a string, and read can parse one back.
program internal_write
implicit none
character(len=32) :: line
real :: mean = 68.14
integer :: day = 3
! An internal write formats into a string instead of the screen.
write (line, '(a, i0, a, f6.2)') "day ", day, " mean ", mean
print *, "built: [", trim(line), "]"
! An internal read parses text back into variables.
read (line(5:5), '(i1)') day
print *, "parsed the day back: ", day
end program internal_write
How it works
write (line, fmt) ...formats intolineinstead of the screen.- The built string prints trimmed, brackets showing its edges.
read (line(5:5), '(i1)')parses the day digit straight out of the text.
Keywords and builtins used here
characterendimplicitintegerlennoneprintprogramreadrealtrimwrite
The run, in numbers
- Lines
- 14
- Characters to type
- 413
- Tokens
- 69
- Three-star pace
- 60 tpm
At the three-star pace of 60 tokens a minute, this run takes about 69 seconds.
Step 3 of 3 in Strings & formats, step 18 of 27 in Language basics.