CS 1001 Lecture 25 Files Internal Files 27

  • Slides: 33
Download presentation
CS 1001 Lecture 25 • Files • Internal Files 27 April, 2000

CS 1001 Lecture 25 • Files • Internal Files 27 April, 2000

File Access • • OPEN the file READ from an existing file WRITE to

File Access • • OPEN the file READ from an existing file WRITE to a new file CLOSE the file 27 April, 2000

OPEN Statement • OPEN (open-list) where open-list is: Must include Unit Specifier Also includes:

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 27 April, 2000 May include : – ERR = Clause – ACCESS = Clause – FORM = Clause – RECL = Clause – BLANK = Clause – PAD = Clause – DELIM = Clause

Unit Specifier • Required – May not be 0, 5, or 6 – 0

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 characterexpression is the filename e. g. , FILE = “file. out” 27 April, 2000

STATUS = Clause • STATUS = character-expression, where characterexpression can have one of the

STATUS = Clause • STATUS = character-expression, where characterexpression 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” 27 April, 2000

IOSTAT = Clause • IOSTAT = status-variable, where status-variable is an integer variable to

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 = i. Open. Error then later on, check i. Open. Error 27 April, 2000

ERR = Clause • ERR = n, where n is the label of an

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 = “, Open. Error 27 April, 2000

READ Statement • READ (control-list) input-list – Must have a Unit Specifier – Must

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) i. Variable, r. Variable, c. Variable 10 FORMAT (I 4, F 7. 2, A 12) 27 April, 2000

END = Clause • END = n, where n is the label of a

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. , 27 April, 2000 END = 40 40 PRINT *, “End of file reached”

Comment on File Reading • In a file like “student. dat” on page A

Comment on File Reading • In a file like “student. dat” on page A 17, 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) i. Stud. Num, c. LName, c. Init, & c. Address, i. Phone, c. Gender, i. Class, c. Major, i. Credits, & i. GPA 10 FORMAT (I 5, A 15, A 1, A 22, I 7, A 1, I 1, A 4, I 3) 27 April, 2000

WRITE Statement • WRITE (control-list) output-list – Must have a Unit Specifier – Usually

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) i. Var, r. Var, c. String 10 FORMAT (I 4, F 7. 2, A 12) 27 April, 2000

ENDFILE Statement • ENDFILE writes into the file a special record called and end-of-file

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 27 April, 2000

CLOSE Statement • CLOSE (close-list) where close-list is: – – – Must include Unit

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 27 April, 2000

SUBROUTINE Read. List (Names, Numbers, Active, Num. Records) INTEGER, INTENT(IN) : : Num. Records

SUBROUTINE Read. List (Names, Numbers, Active, Num. Records) INTEGER, INTENT(IN) : : Num. Records CHARACTER *20, INTENT(OUT) : : Names(Num. Records) CHARACTER *13, INTENT(OUT) : : Numbers(Num. Records) LOGICAL, INTENT(OUT) : : Active(Num. Records) 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, Num. Records READ (1, 2, IOSTAT=Status) Names(i), Numbers(i), Active(i) 2 FORMAT (A 20, A 13, L 1) IF (Status <= 0 ) EXIT END DO ENFILE(1) CLOSE (1) END SUBROUTINE Read. List 27 April, 2000

Internal Files • Sequence of memory locations containing information stored in character form and

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 c. Date = “JULY 4, 1776” c. Year = c. Date(9: 12) READ (UNIT = c. Year, FMT = ‘(I 4)’) i. Year or READ (c. Year, ‘(I 4)’) i. Year or READ (c. Date(9: 12), ‘(I 4)’) i. Year or READ (c. Date, ‘(8 X, I 4)’) i. Year Each of these takes the last four characters in the string c. Date and converts it to an integer format named i. Year 27 April, 2000

OPEN Arguments • Back up slides 27 April, 2000

OPEN Arguments • Back up slides 27 April, 2000

ACTION = Clause • ACTION = i-o-action, where i-o-action is a character expression whose

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” 27 April, 2000

POSITION = Clause • POSITION = character-expression, where the value of character-expression is: –

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” 27 April, 2000

ACCESS = Clause • ACCESS = access-method, where access-method can be either: – “SEQUENTIAL”

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” 27 April, 2000

FORM = Clause • FORM = form-specifier, where form-specifier is a character expression that

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” 27 April, 2000

PAD = Clause • Only applies to FORMATTED files • PAD = character-expression, which

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 27 April, 2000

RECL = Clause • RECL = record-length, where record-length is an integer whose value

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 27 April, 2000

BLANK = Clause • Only applies to FORMATTED files • BLANK = blank-specifier, which

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 27 April, 2000

DELIM = Clause • Only applies to FORMATTED files • DELIM = character-expression, which

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 27 April, 2000

STATUS = Clause • STATUS = character-expression – “KEEP” may not be used for

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 27 April, 2000

READ Arguments • Backup slides 27 April, 2000

READ Arguments • Backup slides 27 April, 2000

ADVANCE = Clause • ADVANCE = character-expression, which can have the values: – “YES”

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 27 April, 2000

IOSTAT = Clause • IOSTAT = integer-variable, where – Positive value if an error

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 27 April, 2000

REC = Clause • REC = integer-expression, where integerexpression is positive and indicates the

REC = Clause • REC = integer-expression, where integerexpression 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 = i. Part. Number 27 April, 2000

WRITE arguments • Backup slides 27 April, 2000

WRITE arguments • Backup slides 27 April, 2000

File-Positioning Statements • • REWIND unit or REWIND position-list BACKSPACE unit or BACKSPACE position-list

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 27 April, 2000

REWIND and BACKSPACE • REWIND positions the file at the beginning of the file’s

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 beginning e. g. , REWIND 8 or REWIND (8, IOSTAT = i. Error) 27 April, 2000

Not Needed For This Class • • INQUIRE Merging Files External Sorting Unformatted Files

Not Needed For This Class • • INQUIRE Merging Files External Sorting Unformatted Files 27 April, 2000