concept write in category fortran

appears as: write
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 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 the read statement to read the user-input data from stdin and store it into text. Finally, we emit messages to stdout and stderr using the write statement. write is a more general and versatile variant of print, and we’ll use it almost exclusively from here on.

The read and write 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 called unit and fmt, 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 and write 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 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

If you look carefully, you’ll notice that the first write statement in the program has advance='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 #1

If omitted, advance has the value yes by default, so any such write statement will move the position to the next line after executing, and likewise for the read statement.

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