file processing
DESCRIPTION
Week 10. FILE PROCESSING. 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. Information for data transfer. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: FILE PROCESSING](https://reader036.vdocuments.us/reader036/viewer/2022062520/56815952550346895dc69146/html5/thumbnails/1.jpg)
1
FILE PROCESSING
Week 10
![Page 2: FILE PROCESSING](https://reader036.vdocuments.us/reader036/viewer/2022062520/56815952550346895dc69146/html5/thumbnails/2.jpg)
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.
![Page 3: FILE PROCESSING](https://reader036.vdocuments.us/reader036/viewer/2022062520/56815952550346895dc69146/html5/thumbnails/3.jpg)
3Information 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)
![Page 4: FILE PROCESSING](https://reader036.vdocuments.us/reader036/viewer/2022062520/56815952550346895dc69146/html5/thumbnails/4.jpg)
4
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. Thus, unit numbers may be said to have global scope.
![Page 5: FILE PROCESSING](https://reader036.vdocuments.us/reader036/viewer/2022062520/56815952550346895dc69146/html5/thumbnails/5.jpg)
5
I / O StatementsI / O Statement Forms
An input or output statement has the following form.
read (unit=2, fmt=”(2i5, a, 2f16.8)”) I, Code, Str, Bugle, Dprwrite (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.
![Page 6: FILE PROCESSING](https://reader036.vdocuments.us/reader036/viewer/2022062520/56815952550346895dc69146/html5/thumbnails/6.jpg)
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
![Page 7: FILE PROCESSING](https://reader036.vdocuments.us/reader036/viewer/2022062520/56815952550346895dc69146/html5/thumbnails/7.jpg)
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
![Page 8: FILE PROCESSING](https://reader036.vdocuments.us/reader036/viewer/2022062520/56815952550346895dc69146/html5/thumbnails/8.jpg)
8
Data TransferRECORDS
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 RECORDSA formatted record is composed of characters. These characters may include letters, digits,
and special symbols from the ASCII character set.
NONADVANCING INPUT AND OUTPUTInput 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)
![Page 9: FILE PROCESSING](https://reader036.vdocuments.us/reader036/viewer/2022062520/56815952550346895dc69146/html5/thumbnails/9.jpg)
9
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
![Page 10: FILE PROCESSING](https://reader036.vdocuments.us/reader036/viewer/2022062520/56815952550346895dc69146/html5/thumbnails/10.jpg)
10
list directed I/O
• syntax
read (unit=1,fmt=*) Inum,Rnum
write (unit=2,fmt=*) Inum,Rnum
-------- 1 2.31 Inum=1 Rnum=2.31
![Page 11: FILE PROCESSING](https://reader036.vdocuments.us/reader036/viewer/2022062520/56815952550346895dc69146/html5/thumbnails/11.jpg)
11
Explicit format controlThe 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, JEach 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 inputread (unit = *, fmt = “(i3, i4, 2 i6, i8, i3)” ) I, J, K, L, M, N
external appearance field width format code internal value123 3 i3 +123+123 4 i4 +123 -123 6 i6 -123- 1203 6 i6 -1203 -12300 8 i8 -12300- - -__ 3 i3 0
![Page 12: FILE PROCESSING](https://reader036.vdocuments.us/reader036/viewer/2022062520/56815952550346895dc69146/html5/thumbnails/12.jpg)
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
![Page 13: FILE PROCESSING](https://reader036.vdocuments.us/reader036/viewer/2022062520/56815952550346895dc69146/html5/thumbnails/13.jpg)
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 -897.6577 f10.3 _ _ -897.658
B 234. f6.0 _ _ 234.
B 234. f8.2 _ _ 234.00
C -0.12 f5.2 -0.12
H 0. f6.3 _ 0.000
![Page 14: FILE PROCESSING](https://reader036.vdocuments.us/reader036/viewer/2022062520/56815952550346895dc69146/html5/thumbnails/14.jpg)
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 -897.6577 10 _ -897.6577
B +234. 8 _ _ + _ 234.
C -0.12 5 - . 12 _
D -897.6577 13 -8.976577E+02
E -0.02032 9 -20.32E-3
F +2.032 8 2032.e-3
G 2032.0 8 2.032E3_
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.
![Page 15: FILE PROCESSING](https://reader036.vdocuments.us/reader036/viewer/2022062520/56815952550346895dc69146/html5/thumbnails/15.jpg)
15
integer I/O
• syntax
read (unit=1,fmt=“(i3,i5)”) Inum,Jnum
write (unit=2,fmt=“(i3,i5)”) Inum,Jnum
---|----- 1 231 Inum=1 Jnum=231
1 22 Inum=10 Jnum=220
![Page 16: FILE PROCESSING](https://reader036.vdocuments.us/reader036/viewer/2022062520/56815952550346895dc69146/html5/thumbnails/16.jpg)
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
---|-----1.0 2.3 Rnum=1.0 Tnum=2.3
1 2.3 Rnum=1.0 Tnum=2.3
![Page 17: FILE PROCESSING](https://reader036.vdocuments.us/reader036/viewer/2022062520/56815952550346895dc69146/html5/thumbnails/17.jpg)
17
real I/O
write (unit=*,fmt=“(e12.4)”) Avar1 01
----------|-- -1.2345e+08
![Page 18: FILE PROCESSING](https://reader036.vdocuments.us/reader036/viewer/2022062520/56815952550346895dc69146/html5/thumbnails/18.jpg)
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)”
---|-----|---|----- 1 231 2 345 Inum1=1 Jnum1=231
Inum2=2 Jnum2=345
![Page 19: FILE PROCESSING](https://reader036.vdocuments.us/reader036/viewer/2022062520/56815952550346895dc69146/html5/thumbnails/19.jpg)
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:
---|-----|---|----- 1 123 2
![Page 20: FILE PROCESSING](https://reader036.vdocuments.us/reader036/viewer/2022062520/56815952550346895dc69146/html5/thumbnails/20.jpg)
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:
---|------|---|----- 1 123 2 34
3 22 4 443
![Page 21: FILE PROCESSING](https://reader036.vdocuments.us/reader036/viewer/2022062520/56815952550346895dc69146/html5/thumbnails/21.jpg)
21
Explicit format controlFORMAT 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)
^ rescan point
![Page 22: FILE PROCESSING](https://reader036.vdocuments.us/reader036/viewer/2022062520/56815952550346895dc69146/html5/thumbnails/22.jpg)
22
positioning
• advance= yes / nowrite (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
![Page 23: FILE PROCESSING](https://reader036.vdocuments.us/reader036/viewer/2022062520/56815952550346895dc69146/html5/thumbnails/23.jpg)
23
positioning
• space x format codewrite (unit=*,fmt=“(4x,’Enter name’,3x)”,advance=“no”)
read (unit=*,fmt=“(a6)”) Name
write (unit=*,fmt=“(3x,’Name entered is’,3x,a6)”) Name
![Page 24: FILE PROCESSING](https://reader036.vdocuments.us/reader036/viewer/2022062520/56815952550346895dc69146/html5/thumbnails/24.jpg)
24
positioning
• tabulate t format codewrite (unit=*,fmt=“(t5,’Enter name’,3x)”,advance=“no”)
read (unit=*,fmt=“(a6)”) Name
write (unit=*,fmt=“(t4,’Name entered is’,t21,a6)”) Name
![Page 25: FILE PROCESSING](https://reader036.vdocuments.us/reader036/viewer/2022062520/56815952550346895dc69146/html5/thumbnails/25.jpg)
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
![Page 26: FILE PROCESSING](https://reader036.vdocuments.us/reader036/viewer/2022062520/56815952550346895dc69146/html5/thumbnails/26.jpg)
26
I/O statusexception handling
• iostat=Vrbl (integer)
Vrbl = 0 => Opr succeeded
Vrbl > 0 => Error
Vrbl < 0 => EOF , EOR
![Page 27: FILE PROCESSING](https://reader036.vdocuments.us/reader036/viewer/2022062520/56815952550346895dc69146/html5/thumbnails/27.jpg)
27
format specification• Format specification with named constantInstead 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
![Page 28: FILE PROCESSING](https://reader036.vdocuments.us/reader036/viewer/2022062520/56815952550346895dc69146/html5/thumbnails/28.jpg)
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)
![Page 29: FILE PROCESSING](https://reader036.vdocuments.us/reader036/viewer/2022062520/56815952550346895dc69146/html5/thumbnails/29.jpg)
29
I / O positioningINPUT 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.
![Page 30: FILE PROCESSING](https://reader036.vdocuments.us/reader036/viewer/2022062520/56815952550346895dc69146/html5/thumbnails/30.jpg)
30I / O positioning
OUTPUT POSITIONINGNow 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.
![Page 31: FILE PROCESSING](https://reader036.vdocuments.us/reader036/viewer/2022062520/56815952550346895dc69146/html5/thumbnails/31.jpg)
31Direct 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.
![Page 32: FILE PROCESSING](https://reader036.vdocuments.us/reader036/viewer/2022062520/56815952550346895dc69146/html5/thumbnails/32.jpg)
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.
![Page 33: FILE PROCESSING](https://reader036.vdocuments.us/reader036/viewer/2022062520/56815952550346895dc69146/html5/thumbnails/33.jpg)
33formatted 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”.
![Page 34: FILE PROCESSING](https://reader036.vdocuments.us/reader036/viewer/2022062520/56815952550346895dc69146/html5/thumbnails/34.jpg)
34formatted 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”.
![Page 35: FILE PROCESSING](https://reader036.vdocuments.us/reader036/viewer/2022062520/56815952550346895dc69146/html5/thumbnails/35.jpg)
35program sicaklikinteger, parameter::satir=365,sutun=27real,dimension(satir,sutun)::sicakreal,dimension(sutun)::ortsaatinteger::i,jopen(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 dodo j=4,sutun ortsaat(j-3)=(sum(sicak(1:satir,j)))/satirend doopen(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