Strings and Internal I/O
Internal I/O
Write Into a Buffer
Internal I/O writes formatted output into a character variable instead of a file or screen.
Program
Play the program to format a number into a string buffer.
internal_io.f90
Replay: real traced execution (multi-file project)
program internal_io
implicit none
character(len=32) :: buf
integer :: count
count = 42
write(buf, '(A, I0)') "count=", count
print '(A)', trim(buf)
end program internal_io
count ← 42
4integer :: count5count = 426write(buf, '(A, I0)') "count=", countvalues this step42countbuf ← count=42
5count = 426write(buf, '(A, I0)') "count=", count7print '(A)', trim(buf)values this stepcount=42buf42countprint '(A)', trim(buf)
6 write(buf, '(A, I0)') "count=", count7 print '(A)', trim(buf)8end program internal_iooutputcount=42values this stepcount=42buf
Follow the Buffer
countstarts as42.write(buf, '(A, I0)') "count=", countwrites intobuf.- The buffer text becomes
count=42. trim(buf)removes unused blanks before printing.- The program prints
count=42. | step | buffer/output | | --- | --- | | number |42| | after internal write |count=42| | printed text |count=42|
internal write
`write(buf, fmt) ...` writes into a character variable.
format
`'(A, I0)'` joins a string and a minimum-width integer.
trim
`trim(buf)` strips trailing blanks before printing.
Exercise: internal_io.f90
Reproduce count=42, then trace which line writes the number into the buffer before it is printed.