Input/Output Statements


What is the difference between a PRINT and a WRITE?

A print always outputs to the "default device" generally the terminal screen. A write sends things to a numbered unit that may be the screen, a disk file, or sometimes a printer (see OPEN statement for connecting unit number to a file).

How do we make sure that all new data is being added to the end of the file "temp.log"?

open (12,file='temp.log', status='old', position='APPEND')

Why does a program with an OPEN statement but no CLOSE statement still compile?

Two reasons. First a CLOSE is not mandatory. The STOP statement quietly looks around for all open units and closes them properly. Secondly, OPEN is a peculiar statement that has effects beyond subprogram boundaries. I can OPEN unit 11 connecting it to a file with the name "input.data" in any subroutine I want. Any other subroutine (function, or portion of the main program) executed after that OPEN will know to direct the results of any WRITE(11,... statements, to the file "input.data".

Can you use an INQUIRE statement to find out if "temp.data" is being read?

Yes, the command "inquire (file='temp.data', number=iunum)" returns a value of -1 if the file is not currently connected to a unit number, or the value of the unit number, if the file has been opened. Its up to you to include logic to decide if that unit number is used for READ or WRITE.

How do you use FORMAT statements and the format number in a write statement?

Always think of the FORMAT and WRITE as a tightly coupled pair. First construct the write statement to put out the values of whatever variables that you need to see (say "j", "x", and "y"). Next think about how you want the numbers to appear in the file or on the screen. If an existing FORMAT gives that result use its number. If not pick any number not currently used as a label and include it in the second position within the WRITE's parentheses (first number is the unit number or a "*" for screen). The statement:

write(11, 2222) j, x, y

writes to the file opened on unit 11 using a FORMAT labeled 2222. Now I construct the format to give the desired appearance of the line. Perhaps I wanted to handle up to 4 digits in the INTEGER "j", standard decimal notation for "x" with 3 digits after the decimal, allowing for values of x between 0 and 9999.999 , and exponential notation for "y" with 4 digits after the decimal, a significant digit before the decimal point, and room for a negative sign in front. I'll also allow at least 5 blank spaces between each number.

2222 format (i4, 5x, f8.3, 5x, 1p, e11.4)

I can insert quoted strings above, if labels are needed for the numbers. The best thing that you can do is to try a few of these with a short test problem.

If you have a number like 11.5967 and you print it with format f4.1 will it output 11.5 or 11.6?

It rounds up to 11.6

What makes the difference when you use different descriptors like i, f, e, p? Could you explain some of these examples like 1p, e9.3, 3x,?

"i" only works if the variable to be output in that position is an INTEGER. "i5" says output an integer in the line allowing 5 spaces to do the job. If the integer is only 2 digits, the first 3 of the 5 spaces are blanks, and the last 2 contain numbers.

"f" only works if the variable is REAL, and puts out a simple decimal number like 1.1, 2.123, etc.

"e" only works if the variable is REAL, and puts out a number in exponential notation like 0.1234e+01, 0.999e-01, etc.

"p" is for use only if you get tired of the leading zero in "e" format. If the variable x contains 1.23400000, then the write:

write(*,1000) x 1000 format(' x = ',1p,e9.3) give a resulting line

x = 1.234e+01 One key item is to get used to synchronizing the use of "i", "f", and "e" with the variables in the WRITE statement. For

write(*,1001) icount, x, y 1001 format ( ' At icount = ', i5, ' x = ', f5.2,5x, 'y = ',1p,e12.3) The "i5" is the first variable descriptor in the FORMAT and tells you how to output the first variable listed after "write(*,1001)" (icount). The "f5.2" is the second variable descriptor in the FORMAT and tells you how to output the second variable after the WRITE (x). The "e12.3" is the third variable descriptor and tells you how to output the third variable being written (y), as modified by the preceding "1P". The "5x" inserts 5 blank spaces between the end of the printed value of "x", and the beginning of the printed string "y ="

Spend some time running format.f and looking at how each combination of WRITE and FORMAT statement produces results on the screen.

How do you get a FORMAT statement to write the titles in the file you write into?

Take a look at the "1000" FORMAT in format.f . Titles involve, making a decision on exactly where you want them in the line, then constructing a FORMAT with the appropriate mix of spacing (X edit descriptors), and quoted strings. I could put this title anywhere, just by changing the unit number on the WRITE statement.

open (11,file='test.out') write(11,1000) 1000 format(14x,' Pressure',13x,'Temperature',17x,'Density') would put the same title string into file "test.out", that format.f wrote to the screen.

How can I format my output to look nicer (clear screen, double space, etc.)

The best tools within Fortran are the in the FORMAT statement. "X" to add spaces in a line, and "/" to add blank lines on the output. Fortran doesn't know directly about your terminal type, so can't issue a specific screen clear command. You can brute force with "////////////////////////" (24 "/"'s) or call the Unix system as in the following example (note the extra call to get a listing of files after the screen is cleared). When clearing screens it is also useful to learn the Fortran "PAUSE" command to pause execution until you hit the RETURN (ENTER) key. Note that 'call system' is specific to the RS6000's. Other systems often have similar Unix connections.

		program test
		call system('clear')
		call system('ls -alF')
		pause
		call system('whoami')
		end
What does // do? (in a Format)

It sticks 2 character strings together into a single string. If char1='file1.in' and char2='file2.out' then when char3=char1(1:5)//char2(6:9) the contents of char3 are 'file1.out'

How about going over Format statements or Write statements that serve as formats.

You're asking for a Chapter or 2 in a text book, but let me cover the basics. A Format is in a sense a language within the Fortran language. It tells the computer how to convert its data into a nice set of characters for display, printout, or further character processing. As such the contents of a format statement are not converted to mysterious machine instructions until they are actually used (for example in a WRITE statement). This opens the possibility of using a WRITE statement to write things into a character variable to form a format variable. The WRITE does not directly serve as a format. It just builds a character string that works as a format somewhere else. For example in

	 CHARACTER*40 MYFORM
	 ...
	 WRITE(MYFORM,2000 ) N
  2000 FORMAT('(''I='',',I1,'I5)')
	 WRITE(6,MYFORM)(II(J),J=1,N)
the first WRITE creates a format MYFORM that is used in the second WRITE. You also learned in examples like this that if you want a single quote to appear as part of a character string, you must enter it within a quoted string as 2 sequential single quotes (''). The only other major new thing that you learned about formats since the last exam is that you can write more numbers than you have specifications for numbers within the format statement. When the last ) of a format is reached, the WRITE wraps around back to the last ( to find out what to do with the remaining numbers. When this wrap-around occurs, a new line is started on the output (like a hidden / in the format). You should still remember what I, E, F, X, and / can do and identify the result of writing things like the numbers 5, 3, 1.1, .03, 1001.1 with a format "(1X,2I5,1P,3E10.3)"

Is there any way to use variables in a format statement? Yes, but you have to use one format statement to build a second using a write to a character string. For example if you want to include the value of "n" as the number of real numbers per line you would do the following:

	 PARAMETER (N=4)
      CHARACTER FORM1*16
      REAL A(N,N)
      DO 10 I=1,N
      DO 10 J=1,N
      A(I,J)=J*100+I
 10   CONTINUE
      WRITE(FORM1,2000)N
 2000 FORMAT('(1X,',I3,'F6.1)')
      WRITE(6,FORM1) A
      STOP
      END


Up to other Questions and Answers / Home


Maintained by John Mahaffy : jhm@psu.edu