Presentation on theme: "Input and Output READ WRITE OPEN. FORMAT statement Format statements allow you to control how data are read or written. Some simple examples: Int=2; real=4.5678."— Presentation transcript:
Input and Output READ WRITE OPEN
FORMAT statement Format statements allow you to control how data are read or written. Some simple examples: Int=2; real= PRINT ‘(I6,2x,F5.1)’,int,real. This will print the following on the screen: (i)Integer in a field of 6 (here 5 spaces + 2) (ii)Two blanks (2x) (iii)Real in a field of 5 with 1 digit after the decimal pt
FORMAT statement again You can define a format either directly by means of: ‘(format-instructions)’ or “(format-instructions)” Or by means of a FORMAT statement (useful if you want to use the same format in several PRINT or WRITE statements. PRINT 30, int1, int2, int3, real1, real2, real3 30FORMAT(1x,3i8,3f10.3) The 30 is a STATEMENT LABEL
Types of format descriptors Iw Integer of width w Fw.dReal of width w with d characters after the. OwOctal of width w (write out no. as octal) ZwHexadecimal of width w Ew.dExponential format e.g e-09. W is the total no. chars, d the no. after the decimal point A or AwCharacter (use A for input and Aw for output) There are others you may need later on, e.g Lw for logical
Example 1 Print ‘(1x, “x=”,f6.2, “ Y=”, e12.3)’,x,y Will print out x as a normal real and y in exponential format with 3 digits after the decimal point The format descriptor nx inserts n blanks The format descriptor tn tabs to the nth character, e.g.: Print 75, “John Q. Doe”, ‘CPSC’, Number 75 FORMAT(1x, A11, T16, A4, 2x, I3) Will produce the output > John Q. Doe CPSC 141
Example 2 A number BEFORE the format descriptor tells the program to repeat it. Thus 5I6means 5 integers of 6 fields 8e10.3means 8 exponential numbers with 3 digits after the decimal point 5(I8,2x,f10.3) repeats the pattern within the brackets Use of a slash within a Format statement forces input or output from a fresh line, e.g. Print ‘(2i3/a20)’,int1,int2,characters puts the characters on a fresh line
Formated READ This is normally used when reading from a file, but will work from the screen also: READ ‘(format-descriptor)’,var1,char1,var2,char2 Note that FORTRAN ignores blanks when reading in variables under format control – so that, for instance, with a format statement i6 the following all get read as 2: 2_____, _2____, __2___, ___2__, ____2_, _____2
Reading character input This is slightly tricky. Some compilers will allow: Character(len=7)::name Read*,name But in general they don’t. You need to use an A format: Read ‘(a)’,name Note that it is not necessary to define the length of the character field – the computer will read in however many characters it needs to fill the declared variable.
File processing: Open Before using a file you need an OPEN statement. This has many options, but you don’t need to know them all now. OPEN(unit,file=file_name,status=‘XXX’, action=‘YYY’) Unit is a NUMBER which identifies this file with a channel that can subsequently be accessed by PRINT, WRITE, READ File_name is the name of the file Status takes values: “OLD”,”NEW”,”REPLACE” or “UNKNOWN”. Action takes values ‘READ’, ‘WRITE’, OR ‘READWRITE’
Examples of Open Open(8,file=‘datasub/file1’) Opens file1 on channel 8 with unknown status, and readwrite access Character(len=20)::filename Filename=‘datasub/file1’ Open(13,file=filename,status=‘old’,action=‘read’) Opens file1 on channel 13 with status old, read only Open(22,file=‘newfile’,status=‘new’,action=‘write’) Creates a blank file called newfile in the current directory, with write access only Avoid using channels 5 or 6 as these are by default the keyboard and screen respectively
Reading from a file Very similar to reading from the screen, except you define the channel: Real,dimension(100)::indata Open(8,file=‘mydatafile’,status=‘old’) Read(8,*) i1,i2,i3,i4 Read in free-format Read(8,’(4i6)’)i1,i2,i3,i4Read under format control Read(8,30)i1,i2,i3,i4 Read(8,’(8f10.3)’)indataReads file directly into array Read(8,’(8f10.3)’)(indata(I),I=1,100)
Writing to a file The Write command is used to write data to a file, rather then the PRINT command. WRITE is very similar to READ: Write(8,*) x1,x2,x3,x4 writes in free format Write(8,’(2f10.3,5x,2e12.5)’)x1,x2,x3,x4 writes under format control Write(8,’(4f10.3)’)x writes out an array Write(8,’(4f10.3)’)(x(j),j=1,4)
Other file commands Close(8) closes a file (releasing the unit no. for a subsequent open command Rewind 8 goes back to the beginning of a file Backspace 8goes back one record. Useful if you’re not sure what kind of data to expect – read the line in as characters, decide what it is then backspace and read again as numbers
Error codes What if you read a file and reach the end of the data? Any further read statement causes the program to crash. It is advisable therefore to allow for this with IOSTAT: Integer :: test Do Read(8,900,iostat=test)x1,x2,x3,x4 If(test.eq.0)then …….. !No problem. Carry on executing program Else if (test.lt.0) then Exit !End of data. Leave do loop Else Print*,’error on reading file’;stop !stop executing program End do
Unformatted read/write Binary (or unformatted) files are much more compact than text (or ascii) files and for very large data files these are preferred. Reading in a binary file is quite easy: READ(8,iostat=test)array - there is no format statement However, you can only read data easily in this way if you used the corresponding write statement to create the file on the same computer (or at least type of computer) – since this maps the file directly to the memory of the computer.
Exercise 1 Write a program to write all the numbers from 1 to 100 into a file. Choose a sensible format Write a program to read this file, multiply each number by pi and write the results out to a new file Write a program to read your name and address from a file and write it to the screen in a different format.
Exercise 2 Write a program to read the data files you created in exercise 1 and convert the numbers from decimal degrees to degrees, minutes and seconds. Put the results into a new file with suitable explanatory text.