BIL106E - PowerPoint PPT Presentation

1 / 21
About This Presentation
Title:

BIL106E

Description:

FILE CONNETION & DISCONNECTION. FILE POSITONNING STATEMENTS. 3. Files and Records. Information that is to be preserved after the execution of a program is ended is ... – PowerPoint PPT presentation

Number of Views:27
Avg rating:3.0/5.0
Slides: 22
Provided by: insIt6
Category:

less

Transcript and Presenter's Notes

Title: BIL106E


1
BIL106E
  • File Processing
  • I/O

2
  • INTRODUCTION
  • FILES RECORDS
  • RECORD TYPES
  • FILE CONNETION DISCONNECTION
  • FILE POSITONNING STATEMENTS

3
Files and Records
  • Information that is to be preserved after the
    execution of a program is ended is stored in a
    file.
  • A file consists of a sequence of records.
  • The records in a file may be accessed in a
    sequential manner or in a random access manner.
  • Writing to a sequential file destroys all records
    after the one written.
  • A file may consist of formatted records and
    optionally one endfile record, or it may consist
    of unformatted records and optionally one endfile
    record.

4
  • A formatted record is written by a formatted
    write statement or by some means external F and
    consists of a sequence of characters it is read
    by a formatted read statement.
  • An unformatted record is written by an
    unformatted write statement and consists of a
    sequence of values in a processor-dependent form
    it is read by unformatted read statement.
  • An endfile is written by an endfile statement.
  • endfile (auxlist)
  • Auxlist consists of a unit specifier and
    optionally an iostat specifier.

5
Formated file example
Title row (record)
Data rows (records)
6
Unformated file example
7
  • INTRODUCTION
  • FILES RECORDS
  • RECORD TYPES
  • FILE CONNETION DISCONNECTION
  • FILE POSITONNING STATEMENTS

8
File Connection Disconnection
  • A file must be connected to a program by an open
    statement before it is first used.
  • An open statement must specify the type of access
    allowed to the file and the position at which
    reading or writing will start in a sequential
    file.
  • open (open_specifier_list)

9
open statement
  • open (open_specifier_list)
  • Some of the specifiers available for use with
    open
  • unitunit_number
  • filefile_name
  • statusfile_status "old", "new" , "scractch",
    "replace"
  • formformat_mode "formatted" or "unformatted"
    actionallowed_actions "read" or "write" or
    "readwrite"
  • access access_type "sequential" or "direct"
  • positionfile_position "rewind" or "append"
  • iostatios
  • reclrecord_length

Mandatory specifier
Study the rules at pages 270-275
10
Examples
  • open(unit9,file"Imagine1",status"old",action"r
    ead",
  • position"rewind")
  • print ,"Please give the name of the output file"
  • read " (a) ",out_file
  • open(unit9,fileout_file,status"old",action"wri
    te"
  • position"append")
  • open(unit9,file"datafile",status"old",action"r
    ead"
  • position"rewind")

11
Examples
  • Open unformated (binary) existing file for
    readingopen (unit 6, file My_Data.bin,
    status old, action read, form
    unformatted)
  • Open formated (text) existing file for
    readingopen (unit J, file My_Input.txt,
    status old, action read)
  • Open direct access file for readingopen (unit
    31, file X23, status old, action read,
    access direct, recl 720)

12
Examples
  • open(unit7,status"scratch",form"unformatted",
  • action"readwrite", position"rewind")
  • open(unit13,file"Problem file",form",status"ol
    d",
  • access"readwrite",position"rewind",iostatios)
  • If(ios / 0) then
  • Print ,"Error during opening of Problem_file"
  • endif
  • !continue processing
  • If a file is connected to a unit then it may not
    also be connected to another unit
  • If a file is connected to a unit then another
    file may not be connected to the same unit.

13

close statement
  • A file which has been connected to a program by
    means of an open statement can be disconnected by
    means of a close statement.
  • close (close_specifiers_list)
  • Possible close specifiers are
  • unit
  • status "keep" or "scratch"
  • iostat

14
  • INTRODUCTION
  • FILES RECORDS
  • RECORD TYPES
  • FILE CONNETION DISCONNECTION
  • FILE POSITONNING STATEMENTS

15
File Positioning Statements
  • There are often situations in which it is
    required to alter the position in a file without
    reading or writing any records.
  • backspace(auxlist) causes the file to be
    positioned just before the preceding record.
  • rewind(auxlist) causes the file to be positioned
    just before the first record so that a subsequent
    input statement will start reading or writing the
    file from the beginning.
  • auxlist unit and iostat (optional) specifiers

16
Example
  • ! Read up to end-of-file
  • do
  • read(unit8,iostatios) dummy
  • if (ioslt0) then
  • exit
  • end do
  • !Backspace to before end-of-file record
  • backspace (unit8)
  • ! Now add new information
  • write(unit8,)
  • ! Terminate file with an end-of-file ready for
    next time
  • endfile (unit8)

17
Example1
  • program fileprg1
  • integer, dimension (10) arr
  • integer i
  • open(unit 1, file"c\filep1.txt",
    status"new", action"write" )
  • do i1,10
  • write( unit1, fmt"(i5)" ) i
  • end do
  • end program fileprg1

18
Example2
  • program fileprg2
  • integer, parameter ARR_SIZE 50
  • real, dimension(ARR_SIZE,2) koord
  • integer, dimension (ARR_SIZE) temp
  • integer sizeInFile
  • integer i, isEOF
  • character (len30) dizi
  • sizeInFile0
  • open(unit 1, file"e\filep2.txt",
    status"old", action"read" )
  • i0
  • read( unit 1, fmt"(a30)" ) dizi
  • print "(7x, a30)", dizi
  • do
  • read( unit 1, fmt"(f5.1,tr1, f5.1,tr1,i2)" ,
    iostatisEOF) koord(i,12), temp(i)
  • if(isEOF lt 0) then
  • exit
  • end if
  • print "(i3, 2f8.2,i3)" ,i, koord(i,1),
    koord(i,2), temp(i)
  • ii1

19
Example3
  • fileprg2a.f in Week10-examples.zip

20
Homework
  • Write a program to access a given input file and
    read formatted data from this file. In given
    formatted input file, first , second and third
    columns indicate the students id number, name
    and scores taken from an exam, respectively.
  • Your program should read the formatted data,
    find the highest score and students id and name
    with the highest score and print them to a
    previously opened output file with the same
    format given in the input file.

21
Input File
  • 10000111 Ali Veli 55
  • 40010247 Ahmet Kutsi 60
  • 80020214 Sezer Yildirim 75
  • 40010257 Ayse Kucuk 65
  • 30010148 Gokhan Akyidiz 88
  • 87010085 Serhat Batmaz 87
  • 88010050 Kerem Rahmi Uzun 65
  • 40001050 Mustafa Hakyemez 54
  • 10002510 Oguz Undes 74
  • 20000250 Sevil Yilmaz 78
Write a Comment
User Comments (0)
About PowerShow.com