Building strings and internal files
A string that is assembled from pieces is the commonest string task there is: a comma-separated line, a label with a number in it, a file name built from a run number. Fortran builds one with //, and it formats the parts with an internal file, which is a write whose destination is a character variable rather than a file.
Building a string
The idiom is a loop and an accumulating string: start with the empty string, append a piece and a separator at every pass, and let the deferred length of the variable grow with the value.
The file build.f90:
program build_demo
implicit none
integer :: i
character(len=20) :: name
character(len=:), allocatable :: csv
csv = ""
do i = 1, 4
write (name, '(a,i0)') "row", i
if (i > 1) csv = csv // ","
csv = csv // trim(name)
end do
print *, "csv : [", csv, "]"
print *, "len :", len(csv)
end program build_demo
gfortran -Wall -Wextra -std=f2018 build.f90 -o build
./build
csv : [row1,row2,row3,row4]
len : 19
The comma is written only when it is not the first piece, which is the simplest way to avoid a leading or a trailing separator. Nineteen characters is four names of four and three commas, and the deferred length made every one of them fit without a declared maximum.
An internal file
write (record, '(i5,f8.2)') 42, 3.5 formats the two values into a character variable with the same edit descriptors a file would use. The string that comes out is the string the program would have written, and a read on it goes back the other way.
program internal_demo
implicit none
character(len=40) :: record
integer :: n
real :: x
write (record, '(i5,f8.2)') 42, 3.5
print *, "record : [", record, "]"
read (record, *) n, x
print *, "n =", n, " x =", x
end program internal_demo
gfortran -Wall -Wextra -std=f2018 internal.f90 -o internal
./internal
record : [ 42 3.50 ]
n = 42 x = 3.50000000
The record is forty characters and the write fills the first thirteen: five for the integer, eight for the real, and the rest of the variable keeps its blanks. The list-directed read on the way back ignores those blanks and reads the two values again, which is the round trip a record needs.
The destination of an internal write must already have a length, so a fixed-length buffer is the normal choice. An allocatable string of deferred length is not sized by the write here: gfortran stops at run time with an end of file error, so the buffer is declared with a length that is known to be enough.
A name from parts
A file name is a prefix, a number and a suffix, and the number has to be zero-padded so that the names sort. i3.3 is the descriptor for that: a field of three characters with at least three digits, which turns 1 into 001.
program name_demo
implicit none
integer :: i
character(len=20) :: fname
character(len=:), allocatable :: log
do i = 1, 3
write (fname, '(a,i3.3,a)') "run_", i, ".txt"
print *, trim(fname)
end do
log = ""
do i = 1, 3
log = log // "run_" // char(48 + i) // " "
end do
print *, "log : [", log, "]"
print *, "len :", len(log)
end program name_demo
gfortran -Wall -Wextra -std=f2018 name.f90 -o name
./name
run_001.txt
run_002.txt
run_003.txt
log : [run_1 run_2 run_3 ]
len : 18
trim(fname) is what the print shows, because the buffer is twenty characters and the name is eleven. The second loop builds the same kind of text from characters rather than numbers: char(48 + i) is the digit for i, since the code of 0 is 48. It is the long way round, and it is worth seeing once, because it is how a program builds text when the piece is a character rather than a number.
The cost of a loop of concatenations
Every csv = csv // piece allocates a new string of the new length and copies the old one into it, so a loop that appends n pieces copies about n squared characters in total. For a line of a few hundred characters that is nothing; for a report built from a million records it is the slowest part of the program.
The cure is a buffer that is allocated once at the size the program can bound, a counter of the characters written, and a substring assignment buf(pos:pos+len(piece)-1) = piece that writes in place. The result is trimmed once at the end, and the routine that parses a record from a file is where that buffer pattern earns its keep.
Exercises
- Change the number of rows to six in build.f90. What is the new length of the csv string?
- Write a record with '(a,i0)' and one with '(a,i5)' for the same number and print both between brackets. How do the two lines differ?
- Build the file name without i3.3 and print the three names. Which order do they sort in?
- Print char(65) and char(97). Which letters are they, and what does that say about the code of a letter?
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does
//do for two strings?Answer
Joins them into a new string of the two lengths added
Concatenation returns the two strings joined end to end.
-
What is the length of
"row" // "12"?Answer
5
The lengths add: three and two.
-
What does this program print?
program p implicit none character(len=20) :: s write (s, "(i3.3)") 7 print *, "[", trim(s), "]" end program pAnswer
[007]
i3.3 pads the number to three digits.
-
What is an internal file?
Answer
A CHARACTER variable used as the unit of a WRITE or READ
The character variable holds the formatted record.
-
What does
char(48 + i)give for i = 3?Answer
The character 3
The code of the digit 0 is 48, so 51 is the digit 3.
-
Why is appending to a string in a loop expensive?
Answer
Each concatenation allocates a new string and copies the old one
The copies add up to a quadratic amount of work.
-
Why is a comma written only when i is greater than one?
Answer
To avoid a leading or trailing separator
The separator belongs between the pieces, not before the first one.
-
Which descriptor pads a number to three digits?
Answer
I3.3
The part after the dot is the minimum number of digits.
-
What must an internal write have before it runs?
Answer
A destination variable that already has a length
The character variable is the file, so it must exist with a size.
-
What does the first loop of
build.f90produce?Answer
row1,row2,row3,row4
A comma is added before every piece except the first.
The programs on this page were built with GNU Fortran (Alpine 15.2.0) 15.2.0 and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.
Article author: Arthur Isaev