27 april, 2000 cs1001 lecture 25 files internal files

33
27 April, 2000 CS1001 Lecture 25 • Files Internal Files

Post on 21-Dec-2015

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 27 April, 2000 CS1001 Lecture 25 Files Internal Files

27 April, 2000

CS1001 Lecture 25

• Files• Internal Files

Page 2: 27 April, 2000 CS1001 Lecture 25 Files Internal Files

27 April, 2000

File Access

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

Page 3: 27 April, 2000 CS1001 Lecture 25 Files Internal Files

27 April, 2000

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

Page 4: 27 April, 2000 CS1001 Lecture 25 Files Internal Files

27 April, 2000

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 filenamee.g., FILE = “file.out”

Page 5: 27 April, 2000 CS1001 Lecture 25 Files Internal Files

27 April, 2000

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”

Page 6: 27 April, 2000 CS1001 Lecture 25 Files Internal Files

27 April, 2000

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

Page 7: 27 April, 2000 CS1001 Lecture 25 Files Internal Files

27 April, 2000

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 filee.g., ERR = 30

30 PRINT *, “File Open Error = “, OpenError

Page 8: 27 April, 2000 CS1001 Lecture 25 Files Internal Files

27 April, 2000

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)

Page 9: 27 April, 2000 CS1001 Lecture 25 Files Internal Files

27 April, 2000

END = Clause

• END = n, where n is the label of a statement to be executed when the end of a sequential file is encounterede.g., END = 40

40 PRINT *, “End of file reached”

Page 10: 27 April, 2000 CS1001 Lecture 25 Files Internal Files

27 April, 2000

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)

Page 11: 27 April, 2000 CS1001 Lecture 25 Files Internal Files

27 April, 2000

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)

Page 12: 27 April, 2000 CS1001 Lecture 25 Files Internal Files

27 April, 2000

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

Page 13: 27 April, 2000 CS1001 Lecture 25 Files Internal Files

27 April, 2000

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

Page 14: 27 April, 2000 CS1001 Lecture 25 Files Internal Files

27 April, 2000

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 (UNIT=1, ACCESS = 'sequential', FILE = Filename, & FORM = 'Formatted', STATUS = ’old') DO i = 1, NumRecords READ (1, 2, IOSTAT=Status) Names(i), Numbers(i), Active(i)2 FORMAT (A20, A13,L1) IF (Status <= 0 ) EXIT END DO ENFILE(1) CLOSE (1) END SUBROUTINE ReadList

Page 15: 27 April, 2000 CS1001 Lecture 25 Files Internal Files

27 April, 2000

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 ExampleGiven cDate = “JULY 4, 1776”

cYear = cDate(9:12)

READ (UNIT = cYear, FMT = ‘(I4)’) iYear

or READ (cYear, ‘(I4)’) iYear

or READ (cDate(9:12), ‘(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

Page 16: 27 April, 2000 CS1001 Lecture 25 Files Internal Files

27 April, 2000

OPEN Arguments

• Back up slides

Page 17: 27 April, 2000 CS1001 Lecture 25 Files Internal Files

27 April, 2000

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”

Page 18: 27 April, 2000 CS1001 Lecture 25 Files Internal Files

27 April, 2000

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”

Page 19: 27 April, 2000 CS1001 Lecture 25 Files Internal Files

27 April, 2000

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”

Page 20: 27 April, 2000 CS1001 Lecture 25 Files Internal Files

27 April, 2000

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”

Page 21: 27 April, 2000 CS1001 Lecture 25 Files Internal Files

27 April, 2000

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

Page 22: 27 April, 2000 CS1001 Lecture 25 Files Internal Files

27 April, 2000

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

Page 23: 27 April, 2000 CS1001 Lecture 25 Files Internal Files

27 April, 2000

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

Page 24: 27 April, 2000 CS1001 Lecture 25 Files Internal Files

27 April, 2000

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

Page 25: 27 April, 2000 CS1001 Lecture 25 Files Internal Files

27 April, 2000

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

Page 26: 27 April, 2000 CS1001 Lecture 25 Files Internal Files

27 April, 2000

READ Arguments

• Backup slides

Page 27: 27 April, 2000 CS1001 Lecture 25 Files Internal Files

27 April, 2000

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

Page 28: 27 April, 2000 CS1001 Lecture 25 Files Internal Files

27 April, 2000

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

Page 29: 27 April, 2000 CS1001 Lecture 25 Files Internal Files

27 April, 2000

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

Page 30: 27 April, 2000 CS1001 Lecture 25 Files Internal Files

27 April, 2000

WRITE arguments

• Backup slides

Page 31: 27 April, 2000 CS1001 Lecture 25 Files Internal Files

27 April, 2000

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

Page 32: 27 April, 2000 CS1001 Lecture 25 Files Internal Files

27 April, 2000

REWIND and BACKSPACE

• REWIND positions the file at the beginning of the file’s 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 beginninge.g., REWIND 8 or REWIND (8, IOSTAT = iError)

Page 33: 27 April, 2000 CS1001 Lecture 25 Files Internal Files

27 April, 2000

Not Needed For This Class

• INQUIRE• Merging Files• External Sorting• Unformatted Files