Presentation is loading. Please wait.

Presentation is loading. Please wait.

Week 10 FILE PROCESSING.

Similar presentations


Presentation on theme: "Week 10 FILE PROCESSING."— Presentation transcript:

1 Week 10 FILE PROCESSING

2 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.

3 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 or 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 (Advancing; if the file on the external device is to be repositioned to the end of the current record. Non advancing; if the file position is to remain in its place within the current record) (Optional) Exception handling to be performed in the event of an end-of-file, end-of-record, or error condition during data transfer. (Optional)

4 I / O Statements Direction of Transfer External Device
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. Thus, unit numbers may be said to have global scope.

5 I / O Statements I / O Statement Forms
An input or output statement has the following form. read (unit=2, fmt=”(2i5, a, 2f16.8)”) I, Code, Str, Bugle, Dpr write (unit=*, fmt=”(f10.3, i6, f10.3,a)”, advance =”no”) A, B, C,” # ” write (unit=*, fmt=*) Code, X+Y+Z 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.

6 I/O statements input read (control list) list
read (unit=1,fmt=“(i4,f3.2)”) Inum,Aver output write (control list) list write (unit=6,fmt=“(i4,f3.2)”) Inum,Aver print (format specifier) list print *, Inum, Aver

7 control list unit specifier : External unit/device to/from data is transferred unit= format specifier : Explicit specification of conversion between internal and external data fmt= positioning, i/o status other specifiers

8 NONADVANCING INPUT AND OUTPUT
Data Transfer RECORDS Information on a file subdivided into records. A record corresponds roughly to a line on a terminal screen or to a printed line. The structure of unformatted records is processor-dependent, since the amount of space required on the external unit depends on the details of processor data representation. FORMATTED RECORDS A formatted record is composed of characters. These characters may include letters, digits, and special symbols from the ASCII character set. NONADVANCING INPUT AND OUTPUT Input and output always positions the input or output file, as a default option, at the end of the last record to or from which information has been transferred. In some cases this style of input and output is not convenient. It would often be preferable to stop data transfer in the middle of an input or output record and to continue later with the remainder of the same record. Instead of default advancing file-positioning style, output statement may request a non advancing style, whereby the file position may remain within a record after the input or output statement has been executed. (See example 9.5, p400, Meissner`s book)

9 LIST-DIRECTED FORMATTING
Format specifier LIST-DIRECTED FORMATTING An asterisk format specifier indicates 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

10 list directed I/O -------- syntax read (unit=1,fmt=*) Inum,Rnum
write (unit=2,fmt=*) Inum,Rnum Inum=1 Rnum=2.31

11 Explicit format control
The following specification describes the arrangement of five fields within a record (3f12.0, 2i3) An input field can be specified by the following format code read (unit=*, fmt=“(3e12.0, 2 i3)”) A, B, C, I, J Each format code in a format description includes the following information: 1) a count, indicating the number of consecutive fields to which it corresponds 2) a conversion mode, indicating by letters such as f, es, i, l or a. 3) a field width 4) a decimal position (optional) I Format code for integer input read (unit = *, fmt = “(i3, i4, 2 i6, i8, i3)” ) I, J, K, L, M, N external appearance field width format code internal value i i i i i - - -__ i

12 format codes * list directed i integer code i4 f real code f6.2
l logical code l e exponential code e11.4 a character code a8

13 Explicit format control
f Format code for real output write (unit = *, fmt = “(t2, f10.3, f6.0, f8.2, f5.2, f6.3)”, A, B, B, C, H real variable internal value format code external appearance A f _ _ B f _ _ 234. B f _ _ C f H f _ 0.000

14 Explicit format control
f Format code for real input read (unit = *, fmt = “(f10.4, f8.0, f5.3, f13.6, f9.0, f8.4, f8.2)”, A, B, C, D, E, F, G real variable internal value field width external appearance A _ B _ _ + _ 234. C _ D E+02 E E-3 F e-3 G E3_ IMPORTANT: If a decimal point appears in the input field, the value of d in the format code is ignored; the actual decimal point overrides the format specification.

15 integer I/O ---|----- syntax read (unit=1,fmt=“(i3,i5)”) Inum,Jnum
write (unit=2,fmt=“(i3,i5)”) Inum,Jnum ---|----- Inum=1 Jnum=231 Inum=10 Jnum=220

16 real I/O ---|----- syntax read (unit=1,fmt=“(f3.0,f5.2)”) Rnum,Tnum
write (unit=1,fmt=“(f3.0,f5.2)”) Rnum,Tnum ---|----- Rnum=1.0 Tnum=2.3 Rnum=1.0 Tnum=2.3

17 real I/O ----------|-- write (unit=*,fmt=“(e12.4)”) Avar 1 01
|-- e+08

18 repetitive I/O code ---|-----|---|----- syntax
read (unit=1,fmt=“(3i3)”) Inum1,Jnum1,Inum2 write (unit=2,fmt=“2(i3,i5)”) Inum1,Jnum1, Inum2,Jnum2 fmt=“2(i3,i5)” .eqv. fmt=“(i3,i5,i3,i5)” fmt=“(3i3)” .eqv. fmt=“(i3,i3,i3)” ---|-----|---|----- Inum1=1 Jnum1=231 Inum2=2 Jnum2=345

19 format longer than list
read (unit=1,fmt=“(3i3)”) Inum1,Jnum1 write (unit=2,fmt=“2(i3,i5)”) Inum1,Jnum1, Inum2 remainings are ignored output ex: ---|-----|---|-----

20 list longer than format
read (unit=1,fmt=“(3i3)”) Inum1,Jnum1,Inum2,Jnum2 write (unit=2,fmt=“2(i3,i5)”) Inum1,Jnum1, Inum2,Jnum2& Inum1,Jnum1, Inum2,Jnum2 format rescan Output ex: ---|------|---|-----

21 Explicit format control
FORMAT RESCAN If there are more list items than format codes in the format description, the input or output process must continue to another record in order to complete the list. For example; suppose that we use the format description (f12.4, i3) which specifies an input record containing only two fields. If five data items are be read, the format must be rescanned to match the remaining items of the list. In the first of the following examples, the rescan point is near the middle of the format; in the second, the rescan point is at the repeat count that immediately follows the opening right parenthesis of the format. (2 (i5, i4, i3), f5.2, 2(i5, i4, i3), f5.2, i6) ^ rescan point (2 ( 2 (i5, i4, i3), f5.2), i6)

22 positioning advance= yes / no
write (unit=*,fmt=“(‘ enter Name and Number‘)”,advance=“no”) read (unit=*,fmt=“(i3,a6)”)) Name,Inum write (unit=*,fmt=“(‘ student with name ‘,a6,’ has nb ‘,i4)”) Name,Inum

23 positioning space x format code
write (unit=*,fmt=“(4x,’Enter name’,3x)”,advance=“no”) read (unit=*,fmt=“(a6)”) Name write (unit=*,fmt=“(3x,’Name entered is’,3x,a6)”) Name

24 positioning tabulate t format code
write (unit=*,fmt=“(t5,’Enter name’,3x)”,advance=“no”) read (unit=*,fmt=“(a6)”) Name write (unit=*,fmt=“(t4,’Name entered is’,t21,a6)”) Name

25 positioning next record ( line ) / format code
write (unit=*,fmt=“(//4x,’Enter name’,3x)”,advance=“no”) read (unit=*,fmt=“(a6)”) Name write (unit=*,fmt=“(/3x,’Name entered is’/,3x,a6/)”) Name

26 I/O status exception handling
iostat=Vrbl (integer) Vrbl = 0 => Opr succeeded Vrbl > 0 => Error Vrbl < 0 => EOF , EOR

27 format specification Format specification with named constant
Instead of a literal character constant, a format specifier may be the name of a character string that contains the format description. program test implicit none real :: A, B, C integer :: J, K character (len = *),parameter::F14="(3f12.0, 2i3)" read (unit = *, fmt = F14) A, B, C, J, K write (unit = *, fmt =F14) A, B, C, J, K stop end program test

28 Files Here some examples for open statement:
open (unit = 6, file =“My_Data.bin”, status =“old”, action =“read”,& form = “unformatted”) open (unit = J, file =“My_Input.txt”, status =“old”, action =“read”) open (unit = 31, file =“X23”, status =“old”, action =“read”, & access =“direct”, recl =720)

29 I / O positioning INPUT POSITIONING
Let’s consider an existing file has been connected to unit 7 for sequential access. A rewind initial position property for the connection was specified in the open statement. Suppose that several read statements are then executed. 1) If the file is unformatted, each read will advance the file by one record. 2) If the file formatted, each advancing read statement will advance the file by one record. 3) Each non advancing read from a formatted file will advance the file when a slash is encountered in the format or when rescan occurs, and the position remains within the current record at the end of execution of the statement. Let us suppose that the end-of-file indicator is encountered. The file is then positioned following the end-of-file indicator, and indication of the end-of-file condition is returned to the program.

30 I / O positioning OUTPUT POSITIONING
Now let’s consider a file that has to be used for sequential output. It is connected to unit 13 and contains no records, no indication of the end-of-file condition. A rewind initial position property for the connection was specified in the open statement. Suppose that several write statements are then executed. 1) If the file is unformatted, each write will add one record to the file. 2) If the file formatted, each advancing write will add at least one record. 3) A non advancing write will add a new record when a slash is encountered in the format or when rescan occurs, but will not end the record after data transfer. After the desired data has been transmitted to the file, the statement end file may be executed to append an end-of-file indicator following the last record written.

31 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.

32 Unformatted input and output
The connection of a file to a unit has a form which is formatted or unformatted. Unformatted files usually correspond to so-called binary files, whereas formatted files correspond to text files. Unformatted records contain information in a processor-dependent form that is obtained by transferring data to and from internal storage without any editing or other transformation. The external representation of the data corresponds exactly to its internal representation. Thus, unformatted data transfer is more efficient because the data is merely copied between the external device and the internal storage of the computer. Formatted records are composed of characters representing data values; a formatted record corresponds to a printed line. Conversion is required between the internal form of the data and its representation on the file.

33 formatted internal data transfer
Formatted internal data transfer changes the representation of data without actually transmitting it to or from an external device. Formatted external data transfer includes two separate processes: a) The change in representation of data between its internal form and its external form. b) The transmission of data to or from the external device. In an internal data transfer statement, the integer expression or asterisk Unit specifier in the control list is replaced by the name of a character variable that serves as a buffer or “internal file”.

34 formatted internal data transfer
Formatted internal data transfer changes the representation of data without actually transmitting it to or from an external device. Formatted external data transfer includes two separate processes: a) The change in representation of data between its internal form and its external form. b) The transmission of data to or from the external device. In an internal data transfer statement, the integer expression or asterisk Unit specifier in the control list is replaced by the name of a character variable that serves as a buffer or “internal file”.

35 program sicaklik integer, parameter::satir=365,sutun=27 real,dimension(satir,sutun)::sicak real,dimension(sutun)::ortsaat integer::i,j open(unit=1,file="d:\f_world\week_10\sicakliklar.txt",status="old", action="read") 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-3)=(sum(sicak(1:satir,j)))/satir open(unit=2,file="d:\f_world\week_10\sicaklik.ort",status="new",action="write") do i=1,sutun-3 write(unit=2,fmt="(f9.3)")ortsaat(i) enddo end program sicaklik


Download ppt "Week 10 FILE PROCESSING."

Similar presentations


Ads by Google