concept text in category fortran

This is an excerpt from Manning's book Modern Fortran: Building efficient parallel applications MEAP V13.
This program consists of essentially three statements: one declaration and two built-in I/O statements. We first declare a character string
text
that will hold the data input from the keyboard. Recall that when declaring a character variable, we need to give it a fixed length a priori, or declare it as a dynamic (allocatable) string. While using the allocatable string would be a more elegant solution here, it would complicate the code quite a bit because Fortran doesn’t allow us to automatically allocate a string on aread
statement. Instead, we’d need to somehow find out the length of the input, allocate the string, and then use theread
statement to store the data into the variable. To keep this example simple, we’ll stick with a fixed-length string. There’s nothing special about the number 1000 here—we’re just making this character variable long enough to hold an unlikely large text input. The first I/O statement isread *, text
. It instructs the computer to read the data from the standard input (more on this in a bit), using default formatting (*
), and store it intotext
. The second I/O statement,print *, trim(text)
, does the same, but the other way around. It takes the value oftrim(text)
and prints it to the screen (terminal) using default formatting (*
).
trim
is the built-in function that removes any trailing blanks. It takes care of the fact thattext
is 1000 characters long, and anything beyond the text input by the user is padded by spaces. What would happen if we didn’t do this? Sincetext
is declared ascharacter(len=1000)
, only the first 10 characters will be occupied by “Greetings!”, and the rest will remain blank. Trimming the blank characters off the end ofmessage
will thus make the output not spill over into the next line if our terminal screen is less than 1000 characters wide (mine is 80).
Figure 6.1. The relationship between the
read
andwrite
statements. In this example, theread
statement reads data from the standard input and writes it into a character variable,text
. Similarly, thewrite
statement writes data from thetext
variable to standard output (screen).![]()
Listing 6.12. Updated version of the quick-note program, which prompts the user if the file exists
language="fortran" linenumbering="unnumbered">program qn use iso_fortran_env, only: stdin => input_unit, & stdout => output_unit implicit none integer :: fileunit character(len=9999) :: filename, text character(len=6) :: pos logical :: file_exists if (command_argument_count() < 1) stop 'Usage: qn <filename>' call get_command_argument(1, filename) inquire(file=trim(filename), exist=file_exists) #1 pos = 'rewind' #2 if (file_exists) then write(stdout, '(a)') & #3 'File ' // trim(filename) // ' already exists!' #3 do write(*, '(a)', advance='no') & #4 '[O]verwrite, [A]ppend, [Q]uit: ' #4 read(stdin, '(a)') text #4 if (any(trim(text) == ['O', 'o'])) then write(stdout, '(a)') & #5 'Overwriting ' // trim(filename) #5 exit #5 else if (any(trim(text) == ['A', 'a'])) then pos = 'append' #6 write(stdout, '(a)') & #6 'Appending to ' // trim(filename) #6 exit #6 else if (any(trim(text) == ['Q', 'q'])) then stop #7 end if end do end if open(newunit=fileunit, file=trim(filename), & action='write', position=pos) do read(stdin, '(a)') text write(fileunit, '(a)') trim(text) flush(fileunit) end do end program qn