concept write in category fortran

This is an excerpt from Manning's book Modern Fortran: Building efficient parallel applications MEAP V13.
This is a rather simple program. We first import standard input, output, and error units from the
iso_fortran_env
module, renaming them on import so that we can use their shorter names, stdin, stdout, and stderr, respectively. We then use theread
statement to read the user-input data from stdin and store it intotext
. Finally, we emit messages to stdout and stderr using thewrite
statement.write
is a more general and versatile variant of
The
read
andwrite
statements as shown in listing 6.4 take two arguments: the first being the I/O unit to read from (or write to), and the second being the format to use to read and write data. These arguments are calledunit
andfmt
, respectively, as illustrated on figure 6.1. In general, you can omit spelling out these keywords in your code if you find them to be too verbose.read
andwrite
statements can take quite a few more optional arguments than this. We’ll take a look at some of them a bit later in this chapter.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).![]()
If you look carefully, you’ll notice that the first
write
statement in the program hasadvance='no'
as a keyword parameter. What this does is make the position in the file not move to the next record (line) after printing the message. As a result, the user will be entering their choice on the same line:./qn daily_todo.txt File daily_todo.txt already exists! [O]verwrite, [A]ppend, [Q]uit: a #1If omitted,
advance
has the valueyes
by default, so any suchwrite
statement will move the position to the next line after executing, and likewise for theread
statement.