concept text in category fortran

appears as: text
Modern Fortran: Building efficient parallel applications MEAP V13

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 a read statement. Instead, we’d need to somehow find out the length of the input, allocate the string, and then use the read 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 is read *, 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 into text. The second I/O statement, print *, trim(text), does the same, but the other way around. It takes the value of trim(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 that text 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? Since text is declared as character(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 of message 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 and write statements. In this example, the read statement reads data from the standard input and writes it into a character variable, text. Similarly, the write statement writes data from the text variable to standard output (screen).
ch06 read write
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
sitemap

Unable to load book!

The book could not be loaded.

(try again in a couple of minutes)

manning.com homepage
test yourself with a liveTest