BIL106E Introduction to Scientific & Engineering Computing 1 List of input edit descriptors...

52
BIL106E Introduction to Scientific & Engineering Computing 1 List of List of input input edit edit descriptors descriptors Descriptor Descriptor Meaning Meaning iw iw = = Read the next w characters as Read the next w characters as integers integers . . fw.d fw.d = = Read the next w characters as a real Read the next w characters as a real number with number with d digits d digits after after the the decimal decimal point point . . aw aw = = Read the next w characters as Read the next w characters as characters characters . . l l w = w = Read the next w characters as a logical Read the next w characters as a logical value value . . t t c = c = Next character to be read is at position Next character to be read is at position c c counting from the very left. counting from the very left. tl tl n = n = Next character to be read is n Next character to be read is n characters characters before the before the current current position position . . trn = trn = Next character to be read is n Next character to be read is n

Transcript of BIL106E Introduction to Scientific & Engineering Computing 1 List of input edit descriptors...

Page 1: BIL106E Introduction to Scientific & Engineering Computing 1 List of input edit descriptors Descriptor Meaning iw = Read the next w characters as integers.

BIL106E Introduction to Scientific & Engineering Computing

1

List of List of inputinput edit descriptors edit descriptorsDescriptor MeaningDescriptor Meaningiw iw = =Read the next w characters as integersRead the next w characters as integers..fw.dfw.d = =Read the next w characters as a real number withRead the next w characters as a real number with d d digitsdigits afterafter thethe decimal pointdecimal point..awaw = =Read the next w characters as charactersRead the next w characters as characters..llw =w =Read the next w characters as a logical valueRead the next w characters as a logical value..ttc =c =Next character to be read is at position cNext character to be read is at position c counting from counting from the very left. the very left.tltln =n =Next character to be read is n charactersNext character to be read is n characters before the before the

current positioncurrent position..trn =trn =Next character to be read is n characters Next character to be read is n characters aafter the fter the currentcurrent positionposition..

Page 2: BIL106E Introduction to Scientific & Engineering Computing 1 List of input edit descriptors Descriptor Meaning iw = Read the next w characters as integers.

BIL106E Introduction to Scientific & Engineering Computing

w: the number of characters to usem: the minimum number of characters to be usedf: the number of digits to the right of the decimal pointe: the number of digits in the exponent

List of List of output edit descriptorsoutput edit descriptors

Integer Iw Iw.m

real

Decimal form Fw.d

Exponential form Ew.d Ew.dEe

Scientific form ESw.d ESw.dEe

Engineering form ENw.d ENw.dEe

logical Lw

character A Aw

Positioning

Horizontal nX

Tabbing Tc TLc, TRc

Vertical /

Others

Grouping r(....)

Format Scanning Control :

Sign Control S, SP, SS

Blank Control BN, BZ

Page 3: BIL106E Introduction to Scientific & Engineering Computing 1 List of input edit descriptors Descriptor Meaning iw = Read the next w characters as integers.

BIL106E Introduction to Scientific & Engineering Computing

3

TnTn causes subsequent output to start at column n.TTrrnn causes a shift to the right by n columns.TTllnn causes a shift to the left by n columns (however, it will not

move the position to the left of column 1).

total=24ave=2.43dev=0.32

write "(t3,a4,i5,tt1515,a8,f4.2,tt3030,a10,f4.2)", &“sum=",total,“average=",ave, “deviation=“,dev

is the same as

write "(t3,a4,i5,ttr4r4,a8,f4.2,ttr4r4,a10,f4.2)",&“sum=",total,“average=",ave, “deviation=“,dev

The print out will be as follows:_ _ sum=_ _ _ 24_ _ _ average=2.43_ _ _ deviation=0.32

3

Page 4: BIL106E Introduction to Scientific & Engineering Computing 1 List of input edit descriptors Descriptor Meaning iw = Read the next w characters as integers.

BIL106E Introduction to Scientific & Engineering Computing

Page 5: BIL106E Introduction to Scientific & Engineering Computing 1 List of input edit descriptors Descriptor Meaning iw = Read the next w characters as integers.

BIL106E Introduction to Scientific & Engineering Computing

program tabular_output real, parameter : : third = 1.0 / 3.0 real : : x integer : : i do i = 1, 10 x = i print “( f 15.4, f 15.4, f 15.4 )”, x, sqrt (x), x**third end doend program tabular_output

Page 6: BIL106E Introduction to Scientific & Engineering Computing 1 List of input edit descriptors Descriptor Meaning iw = Read the next w characters as integers.

BIL106E Introduction to Scientific & Engineering Computing

program test2

character(len=6)::a,b,c

print*,”Please write your name”

read "(a8,t1,a4,t1,a)",a,b,c

print "(a10,tr12,a4,tr30,a)",a,b,c

print "(a,t10,a,t52,a)",a,b,c

end program test2

6

Page 7: BIL106E Introduction to Scientific & Engineering Computing 1 List of input edit descriptors Descriptor Meaning iw = Read the next w characters as integers.

BIL106E Introduction to Scientific & Engineering Computing

program multi_record_example

real :: a,b

a = 12.25

b = 23.50

write(unit=6,fmt="(t10,a,3/,a,f6.2,a,f6.2,a,///,f7.2,2/,a,f10.3)")&

"Multi-record example", &

" The sum of ",a," and",b," is", a+b, &

" Their product is",a*b

end program multi_record_example

7

Page 8: BIL106E Introduction to Scientific & Engineering Computing 1 List of input edit descriptors Descriptor Meaning iw = Read the next w characters as integers.

BIL106E Introduction to Scientific & Engineering Computing8

USING FILES TO PRESERVE DATA

FILE PROCESSING• Files and records• Formatted, unformatted and endfile records• Connecting an external file to your programs and

disconnecting it• File positioning statements

8

Page 9: BIL106E Introduction to Scientific & Engineering Computing 1 List of input edit descriptors Descriptor Meaning iw = Read the next w characters as integers.

BIL106E Introduction to Scientific & Engineering Computing 9

A file is a collection of data records

Page 10: BIL106E Introduction to Scientific & Engineering Computing 1 List of input edit descriptors Descriptor Meaning iw = Read the next w characters as integers.

BIL106E Introduction to Scientific & Engineering Computing10

Introduction• The most convenient way to

process involving large data sets is to store them into a file for later processing.

• In order to work with files we need to focus on I/O operations, data transfer and file operations.

Input (I) Output (O)

10

Page 11: BIL106E Introduction to Scientific & Engineering Computing 1 List of input edit descriptors Descriptor Meaning iw = Read the next w characters as integers.

BIL106E Introduction to Scientific & Engineering Computing11

Information for data transfer

Data transfer characteristics are specified by the following items called control information.

• The direction of data transfer (input or output)• The external device, or unit, to from which

data is to be transferred• The format description that specifies

conversion between the computer-oriented internal form of data and its representation as a character string.

• Positioning action that is to occur at the end of data transfer

11

Page 12: BIL106E Introduction to Scientific & Engineering Computing 1 List of input edit descriptors Descriptor Meaning iw = Read the next w characters as integers.

BIL106E Introduction to Scientific & Engineering Computing12

I / O Statements

Direction of TransferThe direction of transfer is determined by the initial keyword in the statement. The keyword read denotes input, while write denotes output.

External DeviceEach external device is identified with a unique integer value called a unit number. A given unit number represents the same device in the main program and in all of its modules and subprograms.

12

Page 13: BIL106E Introduction to Scientific & Engineering Computing 1 List of input edit descriptors Descriptor Meaning iw = Read the next w characters as integers.

BIL106E Introduction to Scientific & Engineering Computing

13

((I/OI/O) ) StatementsStatements

• inputinput

read (<control_list>) <list>

read (unit = 1, fmt =“(i4,f3.2)”) a, b

• outputoutput

write (<control_list>) <list>

write (unit = 6, fmt =“(i4,f3.2)”) a, b

print (<format_specifier>) <list>

print *, a, b

13

Page 14: BIL106E Introduction to Scientific & Engineering Computing 1 List of input edit descriptors Descriptor Meaning iw = Read the next w characters as integers.

BIL106E Introduction to Scientific & Engineering Computing

14

II/O/O operations in Foperations in Freadreadprintprintwritewrite

openopencloseclose

inquireinquirebackspacebackspaceendfileendfilerewindrewind

actual data transferactual data transfer

connection between an connection between an I/OI/O unit unit and and a filea file

find out things about find out things about an I/O an I/O unit or a fileunit or a file

affect the position of the fileaffect the position of the file

I/OI/O operations deal with files operations deal with files..

14

Page 15: BIL106E Introduction to Scientific & Engineering Computing 1 List of input edit descriptors Descriptor Meaning iw = Read the next w characters as integers.

BIL106E Introduction to Scientific & Engineering Computing

RecordsRecords

A data record is a sequence of values. These values may be represented either formatted or

unformatted. F may read and write three types of data records: (a) formatted, (b) unformatted, (c) end-file records

A formatted record contains only formatted data. A formatted record is composed of characters only. These characters include letters, digits, and special symbols

from the ASCII character set The statement

write(*, “(i1,a,i2)”) 6,”,”,11 would produce a record, which can be schematically

represented as:

6 , 1 1

Page 16: BIL106E Introduction to Scientific & Engineering Computing 1 List of input edit descriptors Descriptor Meaning iw = Read the next w characters as integers.

BIL106E Introduction to Scientific & Engineering Computing

16

RecordsRecordsUnformatted data Unformatted data records records consist of values consist of values represented just as they are stored in computer represented just as they are stored in computer memory. memory.

The structure of unformatted records is processor-The structure of unformatted records is processor-dependent, since the amount of spacedependent, since the amount of space required on required on the external unit depends on the details the external unit depends on the details of the of the data data representationrepresentation on that on that processor. processor.

If integers are stored using an eight bit binary If integers are stored using an eight bit binary representationrepresentation,,

writewrite(9) 6, 11(9) 6, 11 would produce an unformatted record such as:would produce an unformatted record such as:

Unformatted records contain only unformatted data.Unformatted records contain only unformatted data.

00000110 00001011

Decimal pattern

Binary numbers

0 0

1 1

2 10

3 11

4 100

5 101

6 110

7 111

8 1000

9 1001

10 1010

11 1011

12 1100

13 1101

14 1110

15 1111

16 10000

Page 17: BIL106E Introduction to Scientific & Engineering Computing 1 List of input edit descriptors Descriptor Meaning iw = Read the next w characters as integers.

BIL106E Introduction to Scientific & Engineering Computing

17

rread, writeead, write and and printprint statements statements

A more general form of A more general form of the the read statement:read statement:read read ((<<ci_listci_list>>) ) <<input_listinput_list>>

Note that <Note that <ci_listci_list> > stands forstands for the the control information list control information list consisting ofconsisting of the the input unit andinput unit and the the format format..unit specifier : External unit to/from data is transferred unit specifier : External unit to/from data is transferred

unitunit = = <<ioio__unitunit>>uunitnit == 55 ! !default input devicedefault input deviceuunitnit == 6 6 ! !default default outoutput deviceput deviceuunitnit == ** ! !default inputdefault input/output /output devicedeviceformat specifier : Explicit specification offormat specifier : Explicit specification of the the conversion conversion between internal and externalbetween internal and external representations of representations of datadata..

fmtfmt = = < <formatformat>>

17

Page 18: BIL106E Introduction to Scientific & Engineering Computing 1 List of input edit descriptors Descriptor Meaning iw = Read the next w characters as integers.

BIL106E Introduction to Scientific & Engineering Computing

fmtfmt = = <<formatformat>>

Page 19: BIL106E Introduction to Scientific & Engineering Computing 1 List of input edit descriptors Descriptor Meaning iw = Read the next w characters as integers.

BIL106E Introduction to Scientific & Engineering Computing

19

rread, writeead, write and and printprint statements statements

Here are two input statements:Here are two input statements:

read (unit=5, fmt=*) read (unit=5, fmt=*) a,a, b,b, ccread (unit=*, fmt=“read (unit=*, fmt=“((3f6.33f6.3))”) ”) a, b, ca, b, c

Output is essentially the same, but you use print or Output is essentially the same, but you use print or write write

statements instead of statements instead of the the read statementread statement::

write (unit=6, fmt=*) write (unit=6, fmt=*) a,a, b,b, ccprintprint (unit=*, fmt=*) (unit=*, fmt=*) a, b, ca, b, cprint *, print *, aa,, b b,, c c

19

Page 20: BIL106E Introduction to Scientific & Engineering Computing 1 List of input edit descriptors Descriptor Meaning iw = Read the next w characters as integers.

BIL106E Introduction to Scientific & Engineering Computing

20

How to Use How to Use More More PPowerful owerful FFormatormat??

Using the forward Using the forward slashslash, /, in read statements:, /, in read statements:

If you have the following record:If you have the following record:

1234567812345678

45678910114567891011

then the statementthen the statement

read read “(3f2.1,/,3i1)”,“(3f2.1,/,3i1)”, a,a, b,b, c,c, p,p, q,q, rr

will read three real numbers from the first line will read three real numbers from the first line and three integers from the second.and three integers from the second.

20

Page 21: BIL106E Introduction to Scientific & Engineering Computing 1 List of input edit descriptors Descriptor Meaning iw = Read the next w characters as integers.

BIL106E Introduction to Scientific & Engineering Computing

21

Control Information for Data Transfer

Data transfer characteristics are specified by the following items, called control information:

• The direction of data transfer (input or output).• The external device (unit) to or from which data

is to be transferred.• The format description that specifies the

conversion between the computer-oriented internal form of the data and its representation as a character string.

21

Page 22: BIL106E Introduction to Scientific & Engineering Computing 1 List of input edit descriptors Descriptor Meaning iw = Read the next w characters as integers.

BIL106E Introduction to Scientific & Engineering Computing

22

The The uunitnit specifier specifier

To transfer data to or from an external file, the file must be To transfer data to or from an external file, the file must be connected to a unit. connected to a unit.

readread((unit unit = 5) a= 5) a read *, bread *, b

• The unit * specifies a processorThe unit * specifies a processor--dedependentpendent unit number. On input, it is unit number. On input, it is the same unit number that the processor would use if a read statement the same unit number that the processor would use if a read statement appeared without the unit number. On output/ it is the same unit appeared without the unit number. On output/ it is the same unit number that the processor would use if a print statement appeared number that the processor would use if a print statement appeared without the unit number. without the unit number.

• A unit number identifies one and only one unit in an F program. That is, A unit number identifies one and only one unit in an F program. That is, a unit number is global to an entire program; a particular file may be a unit number is global to an entire program; a particular file may be connected to unit 9 in one procedure and referred to through unit 9 in connected to unit 9 in one procedure and referred to through unit 9 in another procedure.another procedure.

22

Page 23: BIL106E Introduction to Scientific & Engineering Computing 1 List of input edit descriptors Descriptor Meaning iw = Read the next w characters as integers.

BIL106E Introduction to Scientific & Engineering Computing

23

The The fmtfmt specifierspecifier

LIST-DIRECTED FORMATTING

An asterisk format (*) specifies list-directed formatting, which is adequate for input in most situations and for output in applications where the precise appearance of the results is not important.

read(unit=5,fmt=*) a, b

write(unit=6,fmt=*) a, b

23

Page 24: BIL106E Introduction to Scientific & Engineering Computing 1 List of input edit descriptors Descriptor Meaning iw = Read the next w characters as integers.

BIL106E Introduction to Scientific & Engineering Computing

24

Unformatted recordUnformatted record

• An unformatted record consists of a sequence of values (in processor-dependent form)

• An unformatted record can only be read by an unformatted input statement

read(unit=9) d,e,f

• write(unit=9) A,B,C produces unformatted records for the values of A, B and C.

24

Page 25: BIL106E Introduction to Scientific & Engineering Computing 1 List of input edit descriptors Descriptor Meaning iw = Read the next w characters as integers.

BIL106E Introduction to Scientific & Engineering Computing

25

Connecting an external file to aConnecting an external file to aprogram and disconnecting itprogram and disconnecting it

• For any information to be transferred between a file and aFor any information to be transferred between a file and a program the file must be connected to a unit. program the file must be connected to a unit. • This connection isThis connection is initiated by means of an open statementinitiated by means of an open statement::

openopen((<<open_specifier_listopen_specifier_list>>))

• The <The <open_specifier_listopen_specifier_list>> consists ofconsists of

unit= unit= <<unit_numberunit_number>>

file= file= <<file_namefile_name>>

status= status= <<filefile__statusstatus>>

fmt = fmt = <<format_modeformat_mode>> action= action= <<allowed_actionsallowed_actions>> position=position= <<file_positionfile_position>>

25

Page 26: BIL106E Introduction to Scientific & Engineering Computing 1 List of input edit descriptors Descriptor Meaning iw = Read the next w characters as integers.

BIL106E Introduction to Scientific & Engineering Computing

26

TheThe status status specifier specifier

• The status specifier is not optional. • It specifies what the status of the file is before the file is opened by the program: status = <filestatus> • <filestatus> is a character expression which is one of old, new,unknown, replace or scratch.

Eg: status=“old” status=“new”

• If the status is old the file must already exist, whereas if it is new then it must not already exist. If new is specified then a new file is created. If it is replace and the file already exists, then it is deleted and an attempt is made to create a new file with the same name. If this is successful, the status is changed to old. If it is replace and the file doesn’t exist, the action will be the same as if new had been specified. If <filestatus> is scratch then a special unnamed file is created for use by the program until when the program ceases execution. Such a file can be used as a temporary file for the duration of execution.

26

Page 27: BIL106E Introduction to Scientific & Engineering Computing 1 List of input edit descriptors Descriptor Meaning iw = Read the next w characters as integers.

BIL106E Introduction to Scientific & Engineering Computing

27

As well as specifying the initial status of the file, it must also bespecified what types of I/O operations are allowed with the file. The action specifier is used for this purpose:

action = <allowed_actions><allowed_actions> is a character expression which must take exactly one of read, write or readwrite.

If <allowed_actions> is read then file is to be treated as a read-only file.

If <allowed_actions> is write then file is to be treated as anoutput file.

If <allowed_actions> is readwrite then all I/O are allowed.

TheThe a actionction specifier specifier27

Page 28: BIL106E Introduction to Scientific & Engineering Computing 1 List of input edit descriptors Descriptor Meaning iw = Read the next w characters as integers.

BIL106E Introduction to Scientific & Engineering Computing

28

The The accessaccess specifier specifier• The The access access specifierspecifier i is optionals optional.. • The The accessaccess specifier specifies the access type that specifier specifies the access type that is permitted to the is permitted to the

filefile::

access access == < <access_typeaccess_type>>

where where <<access_typeaccess_type>> is a character expression is a character expression which which must take one of must take one of the two valuesthe two values: : sequential or direct.sequential or direct.

• If If <<access_typeaccess_type>> is not specified is not specified, then, then the default is the default is (it is understood to (it is understood to be)be) sequential sequential..

• If If <<access_typeaccess_type>> is specified to be sequential, a position is specified to be sequential, a position specifierspecifier must be included to instruct the open statement where must be included to instruct the open statement where the file is to be initially positionedthe file is to be initially positioned. This is discussed next.. This is discussed next.

28

Page 29: BIL106E Introduction to Scientific & Engineering Computing 1 List of input edit descriptors Descriptor Meaning iw = Read the next w characters as integers.

BIL106E Introduction to Scientific & Engineering Computing

29

The The positionposition specifier specifier

• The The position position specifierspecifier i is optionals optional::

ppositionosition == < <file_positionfile_position>>

where where <<file_positionfile_position>> is a character expression which is a character expression which mmustust take one of the valuestake one of the values:: rewindrewind or or appendappend • If the file did not previously If the file did not previously existexist and and <<file_positionfile_position>> is is rewindrewind then the file is positioned at its initial point then the file is positioned at its initial point..• If the fileIf the file does does exist and exist and <<file_positionfile_position>> is is rewindrewind then the file is positioned at its initial point and read statements then the file is positioned at its initial point and read statements

read the first read the first record in the filerecord in the file,, and write statements write a new and write statements write a new first recordfirst record..

• If the file existIf the file existss and and <<file_positionfile_position>> is is appendappend then the file is then the file is positioned positioned just just before the endfile recordbefore the endfile record..

29

Page 30: BIL106E Introduction to Scientific & Engineering Computing 1 List of input edit descriptors Descriptor Meaning iw = Read the next w characters as integers.

BIL106E Introduction to Scientific & Engineering Computing

30

• Files normally have a name by which they are known to the computer system

file = <file_name>

where <file_name> is a character expression which must obey the rules for of a file name for the particular computer system.

• Thus, if the name of the file is given as semih, the statement to connect to this file can be given as:

open(unit=9, file=”semih”, status=“old”, & action=“read”, position= =”rewind”)

ffilesiles

30

Page 31: BIL106E Introduction to Scientific & Engineering Computing 1 List of input edit descriptors Descriptor Meaning iw = Read the next w characters as integers.

BIL106E Introduction to Scientific & Engineering Computing

31

The The openopen sstatementtatement• The open statement establishes a connection between a

unit and an external file and determines the connection properties.

• After this is done, the file can be used for data transfer (reading and writing) using the unit number.

open (unit=6,file=“My_Data.bin”,status=“old”, & action = “read”, form = “unformatted”)

open (unit=8,file=“My_Input.txt”,status =“old”, & action = “read”)

open(unit=31, file=“X23”, status = “old”, & action = “read”)

31

Page 32: BIL106E Introduction to Scientific & Engineering Computing 1 List of input edit descriptors Descriptor Meaning iw = Read the next w characters as integers.

BIL106E Introduction to Scientific & Engineering Computing

32

TheThe closeclose statement statement

• Execution of a close statement terminates the connection of a file to a unit.

• Any connections not closed explicitly by a close statement are closed by the operating system when the program terminates.

• The form of the close statement is:

close(<close_spec_list>)

close(unit=9)

32

Page 33: BIL106E Introduction to Scientific & Engineering Computing 1 List of input edit descriptors Descriptor Meaning iw = Read the next w characters as integers.

BIL106E Introduction to Scientific & Engineering Computing33

Open files

open (open_specifier_list)open(unit=1,file=“datafile”,status=“old”,action=“read”,position=“rewind”)

open(unit=2,file=“yagis.dat”,status=“new”,action=“write”,position=“rewind”)

file=file_name

Print*,”Please give the name of the output file”

Read”(a)”,out_file

open(unit=3,file=out_file,status=“old”,action=“write”,position=“append”)

33

Page 34: BIL106E Introduction to Scientific & Engineering Computing 1 List of input edit descriptors Descriptor Meaning iw = Read the next w characters as integers.

BIL106E Introduction to Scientific & Engineering Computing

program xproduct implicit none integer :: i,j integer, parameter :: out_unit=20 print*,"enter two integers" read (*,*) i,j! open (unit=out_unit,file="results.txt",action="write", status="replace”)

open (unit=out_unit,file="results.txt",action="write",& status="old",position=”append”) write (out_unit,*) "The product of",i," and",j write (out_unit,*) "is",i*j close (out_unit)end program xproduct

34

Page 35: BIL106E Introduction to Scientific & Engineering Computing 1 List of input edit descriptors Descriptor Meaning iw = Read the next w characters as integers.

BIL106E Introduction to Scientific & Engineering Computing35

program add_book !Write a program to create a address bookcharacter(len=15)::name,sname,phone,file_nameprint*,"Please enter address book file name"read"(a15)",file_nameopen(unit=1,file=file_name,status=“unknown",action="readwrite",position=“append")do

print*,"Enter a Name or Enter a Q/q to exit" read*,name if (name=="q" .or. name=="Q") then exit endif print*,"Sur Name" read*,sname print*,"Phone Number" read"(a15)",phone

print*,name,sname,phone write(unit=1,fmt=“(3a15)”)name,sname,phone

enddo end program add_book

35

Page 36: BIL106E Introduction to Scientific & Engineering Computing 1 List of input edit descriptors Descriptor Meaning iw = Read the next w characters as integers.

BIL106E Introduction to Scientific & Engineering Computing36

ahmet aktas 212 247 45 70 ayşe tolun 212 285 31 27 Yusuf Cihan 532 322 69 43

!Write a program to read list of the address book from a file

program read_bookcharacter(len=15)::name,sname,phone,file_nameinteger::iosprint*,"Please enter address book file name"read"(a15)",file_nameopen(unit=2,file=file_name,status="old", & action="readwrite“, position="rewind")do

read(unit=2,fmt="(3a15)“,iostat=ios)name,sname,phoneprint*,name,sname,phoneif (ios<0) then

exitendif

enddoend program read_book

36

Page 37: BIL106E Introduction to Scientific & Engineering Computing 1 List of input edit descriptors Descriptor Meaning iw = Read the next w characters as integers.

BIL106E Introduction to Scientific & Engineering Computing

!This program written by Adem GÜNEL program rehber character(len=15)::dosya,ad,soy,c integer::x,tlf  print*, "What do you want to"  print*,"New record (1)"  print*,"Kayit Oku  (2)"  print*,"Cikis      (3)"   do  read*,x    select case(x)      case(1)  print*,"Kayit Icin Dosya Adi Girin"  read*,dosya      open(unit=1,file=dosya,status="new",action="write")  print*, "adi:"  read*,ad  print*,"Soyadi:"  read*,soy  print*,"Telefonu:"  read*,tlf  print*,"Devam Etmek Istiyormusunuz?(E/H)"  read*,c    select case(c)      case("H","h")        exit   

case default  print*, "adi:"  read*,ad  print*,"Soyadi:"  read*,soy  print*,"Telefonu:"  read*,tlfendselect write(unit=1,fmt=*)ad,soy,tlfclose(unit=1)    case(2) print*, "Dosya Adini Girin." read*,dosya open(unit=2,file=dosya,status="unknown",action="readwrite")  read(unit=2,fmt=*)ad,tlf,soy  print*,ad,soy,tlfclose(unit=2)    case(3)       exit   case default  end select enddoend program rehber

37

Page 38: BIL106E Introduction to Scientific & Engineering Computing 1 List of input edit descriptors Descriptor Meaning iw = Read the next w characters as integers.

BIL106E Introduction to Scientific & Engineering Computing38

program veriokumacharacter(len=25)::veriinteger::dosyasonukontrolopen(unit=12,file="C:\Documents and Settings\torosh\Desktop\sicakliklar.dat",status="old",action="read")do i=1,5read(unit=12,fmt=*,iostat=dosyasonukontrol)veriif (dosyasonukontrol<0) thenexitendifprint*,veriend do

end program veriokuma

38

Page 39: BIL106E Introduction to Scientific & Engineering Computing 1 List of input edit descriptors Descriptor Meaning iw = Read the next w characters as integers.

BIL106E Introduction to Scientific & Engineering Computing39

program sicaklikokuinteger::yil,ay,gun,dsk,i,nreal::s1,s2,s3,s4,s5,s6,s7,s8,s9,s10,s11,s12character(len=100)::ilksatiropen(unit=1,file="sicakliklar.dat",status="old",action="read")open(unit=2,file="sicakliklar.out",status="unknown",action="write")Print*,”Başta kaç satır açıklama var?”read*,nDo i=1,nread(unit=1,fmt="(a)")ilksatirprint*,ilksatirwrite(unit=2,fmt="(a)")ilksatirEnd dodo read(unit=1,fmt="(4x,i4,6x,i2,6x,i2,12(3x,f5.1))",iostat=dsk)yil,ay,gun,s1,s2,s3,s4,s5,s6,s7,s8,s9,s10,s11,s12 if(dsk<0) then exit endif write(unit=*,fmt="(i4,1x,i2,1x,i2,12(1x,f5.1))")yil,ay,gun,s1,s2,s3,s4,s5,s6,s7,s8,s9,s10,s11,s12enddoendprogram sicaklikoku

39

Page 40: BIL106E Introduction to Scientific & Engineering Computing 1 List of input edit descriptors Descriptor Meaning iw = Read the next w characters as integers.

BIL106E Introduction to Scientific & Engineering Computing40

positioning• advance= yes / noprogram file_samp

character(len=12)::Name

integer::Inum

write (unit=*,fmt="(' enter Name and Number')",advance="no")

read (unit=*,fmt="(a6,i3)") Name,Inum

write (unit=*,fmt="(' student with name ',a6,' has number ',i4)") Name,Inum

end program file_samp

The positioning specifier advance=”no” in the write statement states that the output file is not to be advanced to a new record after A, B and C have been written to the file. Thus the second write statement will append additional data to the same output record.

40

Page 41: BIL106E Introduction to Scientific & Engineering Computing 1 List of input edit descriptors Descriptor Meaning iw = Read the next w characters as integers.

BIL106E Introduction to Scientific & Engineering Computing41

positioning

• space x format codeprogram file_samp

character(len=12)::Name

write (unit=*,fmt="(t5,'Enter name',3x)",advance="no")

read (unit=*,fmt="(a6)") Name

write (unit=*,fmt="(t4,'Name entered is',t21,a6)") Name

end program file_samp

41

Page 42: BIL106E Introduction to Scientific & Engineering Computing 1 List of input edit descriptors Descriptor Meaning iw = Read the next w characters as integers.

BIL106E Introduction to Scientific & Engineering Computing42

positioning

• tabulate t format codeprogram file_samp

character(len=12)::Name

write (unit=*,fmt="(t5,'Enter name',3x)",advance="no")

read (unit=*,fmt="(a6)") Name

write (unit=*,fmt="(t4,'Name entered is',t21,a6)") Name

end program file_samp

42

Page 43: BIL106E Introduction to Scientific & Engineering Computing 1 List of input edit descriptors Descriptor Meaning iw = Read the next w characters as integers.

BIL106E Introduction to Scientific & Engineering Computing43

positioning

• next record ( line ) / format code

program file_samp

character(len=12)::Name

write (unit=*,fmt="(//4x,'Enter name',3x)",advance="no")

read (unit=*,fmt="(a6)")Name

write (unit=*,fmt="(/3x,'Name entered is'/,3x,a6/)") Name

end program file_samp

43

Page 44: BIL106E Introduction to Scientific & Engineering Computing 1 List of input edit descriptors Descriptor Meaning iw = Read the next w characters as integers.

BIL106E Introduction to Scientific & Engineering Computing44

I/O statusexception handling

• iostat=Vrbl (integer)

Vrbl = 0 => Opr succeeded

Vrbl > 0 => Error

Vrbl < 0 => EOF , EOR Negative ios means end-of-file

44

Page 45: BIL106E Introduction to Scientific & Engineering Computing 1 List of input edit descriptors Descriptor Meaning iw = Read the next w characters as integers.

BIL106E Introduction to Scientific & Engineering Computing45Direct access input and

outputThe connection of a file to a unit has an access method, which is

sequential or direct. Direct access means that records may be accessed in an arbitrary sequence. Each record of the file has a unique record number that is established when the record is written and does not change. A Record number specifier in the Control List of each read or write statement indicates which record is to be read or written; this specifier has the form

rec = Record number

Record number is an integer expression with a positive value.

Example for direct access output statement:

write (unit = 3, fmt = *, rec = 76) X, Y, Z, Z5

• write statement sends information to record number 76 of unit 3.

45

Page 46: BIL106E Introduction to Scientific & Engineering Computing 1 List of input edit descriptors Descriptor Meaning iw = Read the next w characters as integers.

BIL106E Introduction to Scientific & Engineering Computing46program sicaklik

character(len=100)::ilksatir

integer, parameter::satir=365,sutun=27

real,dimension(satir,sutun)::sicak

real,dimension(sutun)::ortsaat

integer::i,j

open(unit=1,file="sicakliklar.dat",status="old",action="read")

read(unit=1,fmt=*)ilksatir

do i=1,satir

read(unit=1,fmt=*)sicak(i,1:sutun)

print*,sicak(i,1:sutun)

end do

do j=4,sutun

ortsaat(j)=(sum(sicak(1:satir,j)))/satir

end do

open(unit=2,file="sicaklik.ort",status="new", action="write")

do i=4,sutun

write(unit=*,fmt="(a,i2,a,f4.1)")"saat ",i-3," nin ortalaması",ortsaat(i)

write(unit=2,fmt="(a,i2,a,f4.1)")"saat ",i-3," nin ortalaması",ortsaat(i)

enddo

end program sicaklik

1996 10 17 15,2 14,8 14,4 14 13,2 14 13,8 16,5 20,1 20,1 21,8 22 24,2 22 23,3 22,3 20,2 19,3 18,9 18,3 18,5 17,9 17 16,6 18,31996 10 18 15,5 14,6 14,8 14,4 14 13,5 14 15,5 17,4 18,9 20,1 20 19,8 18,8 21 19,6 18,9 17,6 17,2 17 16,8 16,5 16,2 15,6 171996 10 19 17,9 16,2 15,5 15,2 15 15,2 15,4 17,3 18,8 21,8 21 20,2 18 19,8 17,9 18,5 18,1 17 16,2 15,7 14,4 14,5 13,9 13,8 171996 10 20 13,5 13,5 13 13 13 12,8 13 14,5 17,8 18,7 18,6 18,6 19,2 19 18,5 17 16,2 15,5 15 14,6 14,1 14,2 14,3 14,1 15,51996 10 21 14,2 14,3 14,5 14,5 14,5 14,5 14,4 15 15,5 16,2 16 17,1 16 17,2 16,8 16 15,5 15 14,8 14,4 14,5 14,1 14 12,1 151996 10 22 13 12,7 12,1 11,9 11,8 11,3 11,4 11,9 13 15 15,6 15,6 14 14 11 10,8 10,3 10 10 9,7 9,6 9,8 10 10 11,91996 10 23 9,9 9,9 9,9 9,8 8,6 9 9 10,3 12 11,2 10,3 11 11,3 10,8 11,1 11,2 10 10 10 10 10,1 9,8 9,8 9,8 10,2

http://atlas.cc.itu.edu.tr/~toros/bil106/sicakliklar.dat

Page 47: BIL106E Introduction to Scientific & Engineering Computing 1 List of input edit descriptors Descriptor Meaning iw = Read the next w characters as integers.

BIL106E Introduction to Scientific & Engineering Computing47

program readdatafromfiles use readdata implicit none real,dimension(2000,200)::x integer::satir,sutun,i character(len=300)::giris call oku(x,satir,sutun,giris) print*,"veriler okundu" print*,"veri dosya ismi “, giris print*,"verinin satir sayisi“, satir print*,"verinin sutun sayisi“, sutun do i=1,satir write(unit=*,fmt=*) x(i,1:sutun) enddoend program readdatafromfiles

47

Page 48: BIL106E Introduction to Scientific & Engineering Computing 1 List of input edit descriptors Descriptor Meaning iw = Read the next w characters as integers.

BIL106E Introduction to Scientific & Engineering Computing48

! Copyright (c) 1994, 1996 Unicomp, Inc.! Developed at Unicomp, Inc.!Permission to use, copy, modify, and distribute this software is freely granted, provided that this notice is preserved.

program char_count ! These two values are processor dependent. integer, parameter :: end_of_file = -1 integer, parameter :: end_of_record = -2 character (len = 1) :: c integer :: kount, ios kount = 0 print *, "Enter as many characters as you want" print *, "on as many lines as you want." print *, "Enter a Q or an End of File to stop" do read (unit=*, fmt="(a)", advance = "no", iostat = ios) c if (ios == end_of_record) then cycle else if (ios == end_of_file .or. c == "Q") then exit

else kount = kount + 1 end if end do print *, "The number of characters", & in the file is kountend program char_count

48

Page 49: BIL106E Introduction to Scientific & Engineering Computing 1 List of input edit descriptors Descriptor Meaning iw = Read the next w characters as integers.

BIL106E Introduction to Scientific & Engineering Computing49

program e_10_1 integer, dimension(12) :: dizi open ( unit=9, file="deneme01.txt", status="old", & action="readwrite", position="rewind" ) read( unit=9, fmt="(3(4(i2,tr1),/))") dizi print *, dizi close(unit=9) open ( unit=9, file="deneme02.txt", status="new", & action="readwrite", position="rewind" ) write( unit=9, fmt="(4(3(i2,tr1),/))") dizi endfile(unit=9) close(unit=9)end program e_10_1

1 87 872154

2 5 45 453 45 45

495

49

Page 50: BIL106E Introduction to Scientific & Engineering Computing 1 List of input edit descriptors Descriptor Meaning iw = Read the next w characters as integers.

BIL106E Introduction to Scientific & Engineering Computing50

program deneme integer ::i,x character (len=5) ::ad print *, "1-50. kuvvetlerini ogrenmek istediginiz ",& "tamsayinin degerini giriniz." read *, x print "(a,/,a)", "Sonuclarin yazilmasini istediginiz dosyaya ", & "bir ad veriniz (maksimum 5 harfli olabilir.)" read *, ad open (unit=9, file= trim(adjustl(ad)),status="new", & action ="write",position="rewind") do i=1,25 write (unit=9, fmt="(i2,a,i20)") i," inci kuvveti =",x**i end do close (unit=9)end program deneme

50

Page 51: BIL106E Introduction to Scientific & Engineering Computing 1 List of input edit descriptors Descriptor Meaning iw = Read the next w characters as integers.

BIL106E Introduction to Scientific & Engineering Computing51

•Write a program which will read up to 40 integer numbers and print them in the reverse order to that in which they were typed.program invers_order integer::i integer, dimension(40)::j do i=1,40 read*,j(i) end do do i=40,1,-1 print*,j(i) enddoend program invers_order

51

Page 52: BIL106E Introduction to Scientific & Engineering Computing 1 List of input edit descriptors Descriptor Meaning iw = Read the next w characters as integers.

BIL106E Introduction to Scientific & Engineering Computing52

Write a program which produces a table of sin x and cos x for angles x from 0° to 360° in steps of 5°.

program tableofsincosinteger::ido i=0,360,5print*,"For degrees ",i," sin =",sin(i*1.0)," ","cos =",cos(i*1.0)enddoend program tableofsincos

52