17
Input and Output READ WRITE OPEN

Input and Output

Embed Size (px)

DESCRIPTION

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 PRINT ‘(I6,2x,F5.1)’,int,real. This will print the following on the screen: 2 4.6 - PowerPoint PPT Presentation

Citation preview

Page 1: Input and Output

Input and Output

READ

WRITE

OPEN

Page 2: Input and Output

FORMAT statement

Format statements allow you to control how data are read or written. Some simple examples:

Int=2; real=4.5678

PRINT ‘(I6,2x,F5.1)’,int,real.

This will print the following on the screen:

2 4.6

(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

Page 3: Input and Output

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

30 FORMAT(1x,3i8,3f10.3)

The 30 is a STATEMENT LABEL

Page 4: Input and Output

Types of format descriptors

Iw Integer of width w

Fw.d Real of width w with d characters after the .

Ow Octal of width w (write out no. as octal)

Zw Hexadecimal of width w

Ew.d Exponential format e.g. 5.319e-09. W is the total no. chars, d the no. after the decimal

point

A or Aw Character (use A for input and Aw for output)

There are others you may need later on, e.g Lw for logical

Page 5: Input and Output

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’, Number75 FORMAT(1x, A11, T16, A4, 2x, I3)

Will produce the output

> John Q. Doe CPSC 141

Page 6: Input and Output

Example 2A number BEFORE the format descriptor tells the program to repeat it. Thus

5I6 means 5 integers of 6 fields

8e10.3 means 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

Page 7: Input and Output

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

Page 8: Input and Output

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.

Page 9: Input and Output

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’

Page 10: Input and Output

Examples of Open

Open(8,file=‘datasub/file1’)Opens file1 on channel 8 with unknown status, and

readwrite access

Character(len=20)::filenameFilename=‘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 onlyAvoid using channels 5 or 6 as these are by default the keyboard and screen respectively

Page 11: Input and Output

Reading from a fileVery similar to reading from the screen, except you define the channel:

Real,dimension(100)::indataOpen(8,file=‘mydatafile’,status=‘old’)

Read(8,*) i1,i2,i3,i4 Read in free-format

Read(8,’(4i6)’)i1,i2,i3,i4 Read under format control

Read(8,30)i1,i2,i3,i4

Read(8,’(8f10.3)’)indata Reads file directly into arrayRead(8,’(8f10.3)’)(indata(I),I=1,100)

Page 12: Input and Output

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 arrayWrite(8,’(4f10.3)’)(x(j),j=1,4)

Page 13: Input and Output

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 8 goes 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

Page 14: Input and Output

Error codesWhat 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 :: testDo Read(8,900,iostat=test)x1,x2,x3,x4 If(test.eq.0)then …….. !No problem. Carry on executing programElse if (test.lt.0) then Exit !End of data. Leave do loopElse Print*,’error on reading file’;stop !stop executing programEnd do

Page 15: Input and Output

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.

Page 16: Input and Output

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.

Page 17: Input and Output

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.