Write

Statement Type:

The WRITE statement is an executable statement.

Statement Purpose:

The write statement is used to "print" information to the screen, a file or other such output device. It can also be used to assign values to variables internal to the program. There are three types of write statements used to accomplish these tasks. The first is a formatted write, the second is an unformatted write and the third is an internal write. A formatted write is used when you want to display your results in some specific way so that it is easy for you to read. The numbers and characters that you would see with the formatted write are what we are used to seeing in our everyday lives. This contrasts with an unformatted write which will dump numbers and characters to the specified file in their binary representations. This is very useful if you plan on using those numbers again is some other computer program. Finally, an internal write is used along with internal reads to flip numbers between real and character variables. For a better explanation of this, look at the below examples

Required Arguments:

UNIT=

This tells the computer where you want your information sent. An asterisk after this indicates the default unit ( usually your monitor ). An integer value after the equals would indicate a number of a file that you had opened earlier. In reality, you don't need this optional flag since the asterisk or the integer value by itself would work just as well. For example:
		Write (11,*) 'This is an example.'
Is the same as,
		Write (unit = 11, *) 'This is an example'
There is one other thing that can be put in the spot of the unit specifier and that would be a variable argument for an internal read or write.

FMT=

This can have an asterisk, an integer or a character variable after it just like the unit argument. In this case though, the meanings are a little different. If the asterisk appears, it means that compiler can insert whatever format that it thinks is the most appropriate. An integer in this case refers to the line number that the compiler can go to get the appropriate format statement. Finally, if a character variable appears there, it means to use the information stored in that character variable for the desired format ( usually you would have stored a format layout in the variable earlier in the program ).

Optional Arguments:

ERR=

Specifies a label for the program to branch to in case an error is detected during writing

IOSTAT=

This specifies an integer variable that will contain an error condition number. A value of zero means no error. A negative number means the end of the file was reached while a positive number means you have some serious problems were detected. The exact meaning of a given positive number is system dependent so to find out what it means, you'll have to reach for a manual on your specific system.

ADVANCE=

If the argument "no" is used after the equal sign, this will tell the computer to write to the last line, starting at the position where the read stopped. Or in other words, when the computer finishes writing, the cursor won't move to the next line after the one it finished on.

REC=

This argument is used for direct access files only. It will specify which record number to be written to. An integer number is what goes after the equals sign so that the computer knows which record to write to.

Examples

A write with an unspecified format and the default unit:
		write (*,*) 'Fortran is complicated'
A write to a previously declared external file with a specified format:
		write (15,2000) mstress
	2000 format (2x,'The maximum stress in the beam is',f5.2,'psi')

An internal write:

		character*5 info
		real x3
		info = 38.98
		write (x3,*) info

A write that has a character variable as its specified format:
		character*20 info
		integer ldata
		ldata = 75
  		info = '(a,i2,a)'
		write ( *,info )'There are',ldata,' data points.'

Related Topics, Lectures and Examples

Open statement

Format statement

Read statement

Print statement

lectures thirteen, twenty two and twenty three

examples: curvefit.f and odeint.f

Up one level / Home


Written by Jason Wehr : jcw142@psu.edu and Maintained by John Mahaffy : jhm@cac.psu.edu