CS1001 Lecture 25 - PowerPoint PPT Presentation

About This Presentation
Title:

CS1001 Lecture 25

Description:

STATUS = Clause ... IOSTAT = Clause ... ERR = Clause ... – PowerPoint PPT presentation

Number of Views:18
Avg rating:3.0/5.0
Slides: 34
Provided by: plum1
Learn more at: http://web.cs.wpi.edu
Category:
Tags: clause | cs1001 | lecture

less

Transcript and Presenter's Notes

Title: CS1001 Lecture 25


1
CS1001 Lecture 25
  • Files
  • Internal Files

2
File Access
  • OPEN the file
  • READ from an existing file
  • WRITE to a new file
  • CLOSE the file

3
OPEN Statement
  • OPEN (open-list) where open-list is
  • Must include Unit Specifier
  • Also includes
  • FILE Clause
  • STATUS Clause
  • ACTION Clause
  • May include
  • POSITION Clause
  • IOSTAT Clause
  • May include
  • ERR Clause
  • ACCESS Clause
  • FORM Clause
  • RECL Clause
  • BLANK Clause
  • PAD Clause
  • DELIM Clause

4
Unit Specifier
  • Required
  • May not be 0, 5, or 6
  • 0 is standard error output, mapped to unit 6
  • 5 is standard input, your keyboard
  • 6 is standard output, your monitor
  • Has the form UNIT integer-expression or simply
    integer-expression
  • FILE Clause
  • FILE character-expression, where
    character-expression is the filename
  • e.g., FILE file.out

5
STATUS Clause
  • STATUS character-expression, where
    character-expression can have one of the
    following values
  • OLD meaning the file already exists
  • NEW meaning the file is being created
  • REPLACE creates a new file to replace an old file
  • SCRATCH no name for FILE given, creates a work
    file that is deleted upon CLOSE
  • UNKNOWN default if STATUS omitted
  • e.g., STATUS NEW

6
IOSTAT Clause
  • IOSTAT status-variable, where status-variable
    is an integer variable to which is assigned
  • 0 if the file is opened successfully
  • Positive value otherwise, representing the number
    of the error message in a list found in the
    system manuals
  • e.g., IOSTAT iOpenError
  • then later on, check iOpenError

7
ERR Clause
  • ERR n, where n is the label of an executable
    statement that is the next statement executed if
    an error occurs in trying to open the file
  • e.g., ERR 30
  • 30 PRINT , File Open Error ,
    OpenError

8
READ Statement
  • READ (control-list) input-list
  • Must have a Unit Specifier
  • Must have a Format Specifier
  • Optional ADVANCE Clause
  • Optional IOSTAT Clause
  • Optional END Clause
  • Optional ERR Clause, same as OPEN
  • Optional REC Clause for direct-access files
  • e.g., READ (12, 10) iVariable, rVariable,
    cVariable
  • 10 FORMAT (I4, F7.2, A12)

9
END Clause
  • END n, where n is the label of a statement to
    be executed when the end of a sequential file is
    encountered
  • e.g., END 40
  • 40 PRINT , End of file reached

10
Comment on File Reading
  • In a file like student.dat on page A17, to read
    in a last name, like Peters, you must read in
  • All of the lines (records) up to that line
  • All of the line itself
  • READ (12, 10) iStudNum, cLName, cName, cInit,
    cAddress, iPhone, cGender, iClass, cMajor,
    iCredits, iGPA
  • 10 FORMAT (I5, A15, A15, A1, A22, I7, A1, I1, A4,
    I3, I3)

11
WRITE Statement
  • WRITE (control-list) output-list
  • Must have a Unit Specifier
  • Usually has a Format Specifier
  • Optional ERR Clause
  • Optional IOSTAT Clause
  • Optional REC Clause
  • e.g., WRITE (13, 10) iVar, rVar, cString
  • 10 FORMAT (I4, F7.2, A12)

12
ENDFILE Statement
  • ENDFILE writes into the file a special record
    called and end-of-file record
  • When encountered by a READ statement
  • IOSTAT clause condition can be detected
  • END clause statement can be executed
  • Once encountered, no more data can be transferred
    to or from this file unless the file is
    repositioned using REWIND or BACKSPACE

13
CLOSE Statement
  • CLOSE (close-list) where close-list is
  • Must include Unit Specifier, same as OPEN
  • May include IOSTAT Clause, same as OPEN
  • May include ERR Clause, same as OPEN
  • May include STATUS Clause
  • All files that are not closed by means of a CLOSE
    statement are automatically closed when an END or
    STOP statement is executed

14
SUBROUTINE ReadList (Names, Numbers, Active,
NumRecords) INTEGER, INTENT(IN)
NumRecords CHARACTER 20, INTENT(OUT)
Names(NumRecords) CHARACTER 13,
INTENT(OUT) Numbers(NumRecords)
LOGICAL, INTENT(OUT) Active(NumRecords)
CHARACTER 20 Filename INTEGER
i ,Status PRINT , 'Enter filename of
input file ' READ , Filename
OPEN (UNIT1, ACCESS 'sequential', FILE
Filename, FORM 'Formatted',
STATUS old') DO i 1, NumRecords
READ (1, 2, IOSTATStatus)
Names(i), Numbers(i), Active(i) 2
FORMAT (A20, A13,L1) IF (Status
lt 0 ) EXIT END DO ENFILE(1)
CLOSE (1) END SUBROUTINE ReadList
15
Internal Files
  • Sequence of memory locations containing
    information stored in character form and named by
    a character variable, array, or array element
  • Used to convert character information to numeric
    form
  • Used to convert character information into a
    character array
  • Internal File Example
  • Given cDate JULY 4, 1776
  • cYear cDate(912)
  • READ (UNIT cYear, FMT (I4)) iYear
  • or READ (cYear, (I4)) iYear
  • or READ (cDate(912), (I4)) iYear
  • or READ (cDate, (8X, I4)) iYear
  • Each of these takes the last four characters in
    the string cDate and converts it to an integer
    format named iYear

16
OPEN Arguments
  • Back up slides

17
ACTION Clause
  • ACTION i-o-action, where i-o-action is a
    character expression whose value is one of
  • READ which opens the file for reading only
  • WRITE which opens the file for writing only
  • READWRITE which opens the file for reading and
    writing
  • e.g., ACTION READ

18
POSITION Clause
  • POSITION character-expression, where the value
    of character-expression is
  • REWIND which positions the file at its initial
    point (beginning), default for new file
  • APPEND which positions the file at its end
  • ASIS leaves its position unchanged, default for
    an existing file already open
  • e.g., POSITION REWIND

19
ACCESS Clause
  • ACCESS access-method, where access-method can
    be either
  • SEQUENTIAL meaning a file is accessed
    sequentially, without defined records
  • DIRECT for record oriented files, where each
    record has a record number for access
  • e.g., ACCESS SEQUENTIAL

20
FORM Clause
  • FORM form-specifier, where form-specifier is a
    character expression that is either
  • FORMATTED
  • UNFORMATTED
  • If omitted, file is assumed formatted if
    sequential, unformatted if direct-access
  • e.g., FORM FORMATTED

21
PAD Clause
  • Only applies to FORMATTED files
  • PAD character-expression, which can have the
    values
  • YES, means the input proceeds as if the actual
    record were padded with blanks between fields
  • NO
  • e.g., PAD YES is the default

22
RECL Clause
  • RECL record-length, where record-length is an
    integer whose value must be positive
  • Only used for direct-access files and specifies
    the length of the records in the file
  • In a formatted file, the record length is the
    number of characters in a record
  • e.g., RECL 25

23
BLANK Clause
  • Only applies to FORMATTED files
  • BLANK blank-specifier, which can be
  • ZERO
  • NULL
  • The first causes blanks in numeric fields to be
    interpreted as zeros, the other causes blanks to
    be ignored.
  • BLANK NULL is default

24
DELIM Clause
  • Only applies to FORMATTED files
  • DELIM character-expression, which can be
  • APOSTROPHE is to be used as the delimiter for
    character strings written to a file
  • QUOTE is to be used as the delimiter for
    character strings written to a file
  • NONE
  • DELIM NONE is the default

25
STATUS Clause
  • STATUS character-expression
  • KEEP may not be used for SCRATCH files.
    Otherwise the state of existence of the file
    remains unchanged (not deleted).
  • DELETE is the default if this clause is
    omitted, however, it depends upon a PERMANENCE
    property which can be TEMPORARY or PERMANENT
  • STATUS is usually not used

26
READ Arguments
  • Backup slides

27
ADVANCE Clause
  • ADVANCE character-expression, which can have
    the values
  • YES specifies that output should advance to a
    new line after the current output has been
    completed
  • NO does not advance the output
  • ADVANCE YES is the default

28
IOSTAT Clause
  • IOSTAT integer-variable, where
  • Positive value if an error occurs, usually the
    error number from a list in a manual
  • Negative value if the end of data occurs but no
    input error occurs
  • 0 if neither an input error nor an end of data
    occurs

29
REC Clause
  • REC integer-expression, where
    integer-expression is positive and indicates the
    number of the record to be read from a
    direct-access file
  • Control list may not contain both an END clause
    and a REC clause
  • e.g., REC iPartNumber

30
WRITE arguments
  • Backup slides

31
File-Positioning Statements
  • REWIND unit or REWIND position-list
  • BACKSPACE unit or BACKSPACE position-list
  • ENDFILE unit or ENDFILE position-list
  • Where position-list
  • Must contain unit or UNIT unit
  • May contain ERR clause
  • May contain IOSTAT clause

32
REWIND and BACKSPACE
  • REWIND positions the file at the beginning of the
    files first record
  • BACKSPACE positions the file at the beginning of
    the preceding record or line
  • Neither has an affect if the file is already at
    the beginning
  • e.g., REWIND 8 or REWIND (8, IOSTAT iError)

33
Not Needed For This Class
  • INQUIRE
  • Merging Files
  • External Sorting
  • Unformatted Files
Write a Comment
User Comments (0)
About PowerShow.com