Download presentation
Presentation is loading. Please wait.
Published byBarrie Howard Modified over 9 years ago
1
BIL106E Introduction to Scientific & Engineering Computing 1 List of input edit descriptors Descriptor Meaning iw = Read the next w characters as integers. fw.d = Read the next w characters as a real number with d digits after the decimal point. aw =Read the next w characters as characters. lw = Read the next w characters as a logical value. tc = Next character to be read is at position c counting from the very left. tln = Next character to be read is n characters before the current position. trn = Next character to be read is n characters after the current position.
2
BIL106E Introduction to Scientific & Engineering Computing w: the number of characters to use m: the minimum number of characters to be used f: the number of digits to the right of the decimal point e: the number of digits in the exponent List of output edit descriptors IntegerIwIw.m real Decimal formFw.d Exponential formEw.dEw.dEe Scientific formESw.dESw.dEe Engineering formENw.dENw.dEe logicalLw characterAAw Positioning HorizontalnX TabbingTcTLc, TRc Vertical/ Others Groupingr(....) Format Scanning Control: Sign ControlS, SP, SS Blank ControlBN, BZ
3
BIL106E Introduction to Scientific & Engineering Computing 3 Tn Trn Tln Tncauses subsequent output to start at column n. Trncauses a shift to the right by n columns. Tln causes a shift to the left by n columns (however, it will not move the position to the left of column 1). total=24 ave=2.43 dev=0.32 t15t30 write "(t3,a4,i5,t15,a8,f4.2,t30,a10,f4.2)", & “sum=",total,“average=",ave, “deviation=“,dev is the same as tr4tr4 write "(t3,a4,i5,tr4,a8,f4.2,tr4,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
4
BIL106E Introduction to Scientific & Engineering Computing
5
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 do end program tabular_output
6
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
7
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
8
BIL106E Introduction to Scientific & Engineering Computing 8 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
9
BIL106E Introduction to Scientific & Engineering Computing 9 A file is a collection of data records
10
BIL106E Introduction to Scientific & Engineering Computing 10 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
11
BIL106E Introduction to Scientific & Engineering Computing 11 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
12
BIL106E Introduction to Scientific & Engineering Computing 12 I / O Statements Direction of Transfer The direction of transfer is determined by the initial keyword in the statement. The keyword read denotes input, while write denotes output. External Device Each 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
13
BIL106E Introduction to Scientific & Engineering Computing 13 (I/O) Statements input input read ( ) read (unit = 1, fmt =“(i4,f3.2)”) a, b output output write ( ) write (unit = 6, fmt =“(i4,f3.2)”) a, b print ( ) print *, a, b 13
14
BIL106E Introduction to Scientific & Engineering Computing 14 I/O operations in F readprintwriteopencloseinquirebackspaceendfilerewind actual data transfer connection between an I/O unit and a file find out things about an I/O unit or a file affect the position of the file I/O operations deal with files. 14
15
BIL106E Introduction to Scientific & Engineering ComputingRecords 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,11
16
BIL106E Introduction to Scientific & Engineering Computing 16 Records Unformatted data records consist of values represented just as they are stored in computer memory. The structure of unformatted records is processor- dependent, since the amount of space required on the external unit depends on the details of the data representation on that processor. If integers are stored using an eight bit binary representation, write(9) 6, 11 write(9) 6, 11 would produce an unformatted record such as: Unformatted records contain only unformatted data. 0000011000001011 Decimal pattern Binary numbers 00 11 210 311 4100 5101 6110 7111 81000 91001 101010 111011 121100 131101 141110 151111 1610000
17
BIL106E Introduction to Scientific & Engineering Computing 17 read, write and print statements A more general form of the read statement: read ( ) read ( ) Note that stands for the control information list consisting of the input unit and the format. unit specifier : External unit to/from data is transferred unit = unit = unit = 5 !default input device unit = 6 !default output device unit = * !default input/output device format specifier : Explicit specification of the conversion between internal and external representations of data. fmt = fmt = 17
18
BIL106E Introduction to Scientific & Engineering Computing fmt = fmt =
19
BIL106E Introduction to Scientific & Engineering Computing 19 read, write and print statements Here are two input statements: read (unit=5, fmt=*) a, b, c read (unit=*, fmt=“(3f6.3)”) a, b, c Output is essentially the same, but you use print or write statements instead of the read statement: write (unit=6, fmt=*) a, b, c print (unit=*, fmt=*) a, b, c print *, a, b, c 19
20
BIL106E Introduction to Scientific & Engineering Computing 20 How to Use More Powerful Format? Using the forward slash, /, in read statements: If you have the following record: 12345678 12345678 4567891011 4567891011 then the statement read “(3f2.1,/,3i1)”, a, b, c, p, q, r will read three real numbers from the first line and three integers from the second. 20
21
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
22
BIL106E Introduction to Scientific & Engineering Computing 22 The unit specifier To transfer data to or from an external file, the file must be connected to a unit. read(unit = 5) a read *, b read *, b The unit * specifies a processor-dependent unit number. On input, it is 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 number that the processor would use if a print statement appeared without the unit number. The unit * specifies a processor-dependent unit number. On input, it is 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 number that the processor would use if a print statement appeared without the unit number. 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 connected to unit 9 in one procedure and referred to through unit 9 in another procedure. 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 connected to unit 9 in one procedure and referred to through unit 9 in another procedure. 22
23
BIL106E Introduction to Scientific & Engineering Computing 23 The fmt specifier 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
24
BIL106E Introduction to Scientific & Engineering Computing 24 Unformatted 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
25
BIL106E Introduction to Scientific & Engineering Computing 25 Connecting an external file to a program and disconnecting it For any information to be transferred between a file and a For 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 is initiated by means of an open statement: This connection is initiated by means of an open statement: open( ) open( ) The consists of The consists of unit= unit= file= file= status= status= fmt = fmt = action= action= position= position= 25
26
BIL106E Introduction to Scientific & Engineering Computing 26 The status 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 = 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 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
27
BIL106E Introduction to Scientific & Engineering Computing 27 As well as specifying the initial status of the file, it must also be specified what types of I/O operations are allowed with the file. The action specifier is used for this purpose: action = is a character expression which must take exactly one of read, write or readwrite. If is read then file is to be treated as a read-only file. If is write then file is to be treated as an output file. If is readwrite then all I/O are allowed. The action specifier 27
28
BIL106E Introduction to Scientific & Engineering Computing 28 The access specifier The access specifier is optional. The access specifier is optional. The access specifier specifies the access type that is permitted to the file: The access specifier specifies the access type that is permitted to the file: access = access = where is a character expression which must take one of the two values: sequential or direct. If is not specified, then the default is (it is understood to be) sequential. If is not specified, then the default is (it is understood to be) sequential. If is specified to be sequential, a position If is specified to be sequential, a position specifier must be included to instruct the open statement where the file is to be initially positioned. This is discussed next. 28
29
BIL106E Introduction to Scientific & Engineering Computing 29 The position specifier The position specifier is optional: The position specifier is optional: position = position = where is a character expression which must take one of the values: rewind or append must take one of the values: rewind or append If the file did not previously exist and If the file did not previously exist and is rewind then the file is positioned at its initial point. is rewind then the file is positioned at its initial point. If the file does exist and is rewind If the file does exist and is rewind 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 record in the file, and write statements write a new first record. If the file exists and is append then the file is If the file exists and is append then the file is positioned just before the endfile record. 29
30
BIL106E Introduction to Scientific & Engineering Computing 30 Files normally have a name by which they are known to the computer system file = where 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”) files 30
31
BIL106E Introduction to Scientific & Engineering Computing 31 The open statement 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
32
BIL106E Introduction to Scientific & Engineering Computing 32 The close 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(unit=9) 32
33
BIL106E Introduction to Scientific & Engineering Computing 33 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
34
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
35
BIL106E Introduction to Scientific & Engineering Computing 35 program add_book !Write a program to create a address book character(len=15)::name,sname,phone,file_name print*,"Please enter address book file name" read"(a15)",file_name open(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
36
BIL106E Introduction to Scientific & Engineering Computing 36 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_book character(len=15)::name,sname,phone,file_name integer::ios print*,"Please enter address book file name" read"(a15)",file_name open(unit=2,file=file_name,status="old", & action="readwrite“, position="rewind") do read(unit=2,fmt="(3a15)“,iostat=ios)name,sname,phone print*,name,sname,phone if (ios<0) then exit endif enddo end program read_book 36
37
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*,tlf endselect write(unit=1,fmt=*)ad,soy,tlf close(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,tlf close(unit=2) case(3) exit case default end select enddo end program rehber 37
38
BIL106E Introduction to Scientific & Engineering Computing 38 program veriokuma character(len=25)::veri integer::dosyasonukontrol open(unit=12,file="C:\Documents and Settings\torosh\Desktop\sicakliklar.dat",status="old",action="read") do i=1,5 read(unit=12,fmt=*,iostat=dosyasonukontrol)veri if (dosyasonukontrol<0) then exit endif print*,veri end do end program veriokuma 38
39
BIL106E Introduction to Scientific & Engineering Computing 39 program sicaklikoku integer::yil,ay,gun,dsk,i,n real::s1,s2,s3,s4,s5,s6,s7,s8,s9,s10,s11,s12 character(len=100)::ilksatir open(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*,n Do i=1,n read(unit=1,fmt="(a)")ilksatir print*,ilksatir write(unit=2,fmt="(a)")ilksatir End do do 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,s12 enddo endprogram sicaklikoku 39
40
BIL106E Introduction to Scientific & Engineering Computing 40 positioning advance= yes / no program 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
41
BIL106E Introduction to Scientific & Engineering Computing 41 positioning space x format code program 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
42
BIL106E Introduction to Scientific & Engineering Computing 42 positioning tabulate t format code program 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
43
BIL106E Introduction to Scientific & Engineering Computing 43 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
44
BIL106E Introduction to Scientific & Engineering Computing 44 I/O status exception handling iostat=Vrbl (integer) Vrbl = 0 => Opr succeeded Vrbl > 0 => Error Vrbl EOF, EOR Negative ios means end-of-file 44
45
BIL106E Introduction to Scientific & Engineering Computing 45 Direct access input and output The 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
46
BIL106E Introduction to Scientific & Engineering Computing 46 program 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 http://atlas.cc.itu.edu.tr/~toros/bil106/sicakliklar.dat
47
BIL106E Introduction to Scientific & Engineering Computing 47 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) enddo end program readdatafromfiles 47
48
BIL106E Introduction to Scientific & Engineering Computing 48 ! 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 kount end program char_count 48
49
BIL106E Introduction to Scientific & Engineering Computing 49 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 187872154 254545 34545495 49
50
BIL106E Introduction to Scientific & Engineering Computing 50 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
51
BIL106E Introduction to Scientific & Engineering Computing 51 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) enddo end program invers_order 51
52
BIL106E Introduction to Scientific & Engineering Computing 52 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 tableofsincos integer::i do i=0,360,5 print*,"For degrees ",i," sin =",sin(i*1.0)," ","cos =",cos(i*1.0) enddo end program tableofsincos 52
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.