Start Homework 9. Read Chpts. 9, 10, and 13, review some of the I/O that bothered you in past examples and study noadv.f
Formatted and Unformatted records, READs and WRITEs. Sequential and Direct Access files. More optional arguments to WRITE, READ, OPEN , and CLOSE. BACKSPACE, REWIND, INQUIRE, and ENDFILE statements.
I've been trying to break you into the various commands for input and output gradually. It's time for a larger infusion of information. However, please be aware that there are a very large number of optional arguments to most of these I/O statements. I am only going to cover some of the more useful. If you can't get one of my structures to do a job properly, take a careful look at a full Fortran manual for your particular compiler before giving up.
It is useful to put Fortran Input/Output into a historical context. The language was developed for scientific calculations, and science often involves analyzing large amounts of data. The dominant storage medium for data was once single reel magnetic tape. This helps explain some of the nomenclature and the need for such things as checking on successful read. It also explains the large variety of options in I/O related commands
I will introduce some additional optional arguments for READ, WRITE, and OPEN. In addition, I will cover the INQUIRE, BACKSPACE, REWIND, and ENDFILE . However, before going any further we need to talk about types of files, and some basic terminology.
The first concept to grasp is the record. Translate that as "line" in a file. When you are typing text with an editor, you create an end of record by hitting the Return (Enter) key. Generally, each time you issue a WRITE statement from Fortran, you create a record. Without special intervention, each time you finish a READ statement, it sets the computer's position pointer to the beginning of the next record. As a result the next READ starts reading from beginning of the next available record in the file, even if the last READ didn't pick up all of the data in its record. An exception to this rule occurs when using the Fortran 90 ADVANCE='NO' optional argument (see noadv.f) with READ or WRITE. Records usually are formatted (contain standard ANSI text). It is also possible to write unformatted records. Here the bytes in the record on disk contain an exact image of the bytes in computer memory for each variable written. You can store more significant digits in a given amount of space with unformatted output, but good luck trying to read the numbers with a text editor. Unformatted, records are only used for information that a program may need later on re-execution, or that another related program will need for further processing.
Fortran supports two general file types: sequential and direct access (as do most other respectable languages). In this class we will be dealing with sequential files. The basic concept here is that to use any information within the file, you must move sequentially through all preceding information (either when reading or writing). The reason for this is that sequential records have variable lengths on disk. I can't determine where the 93rd record begins on the disk without working my way through the preceding 92 records. Direct access files avoid this problem by requiring all records to have the same length. In this case the calculation of the starting point for the 93rd record is simple, but depending on the application, I may pay a high price in wasted disk space, because I must set my fixed record length to accommodate the longest possible record of information.
Specifies a label to which the program will branch if any error is detected during the READ or WRITEIOSTAT=
Specifies an INTEGER variable that will contain an error condition number. A value of zero means no error. A negative value indicates that the End of File or End of Record (you tried to read more numbers than existed in the record) was reached. Negative values are returned for End of File and End of Record, but their precise values(or value) are not specified in the Fortran standard. A positive value indicates a serious problem, but the precise meaning associated with a given number is system dependent and defined in the specific compiler manual. The presence of this argument will prevent termination of a program if an error occurs during READ or WRITE. It is up to you to handle the error.ADVANCE= (Fortran 90 only)
ADVANCE='NO' tells a READ or WRITE not to advance the file position pointer to the next record when done. The effect for a READ is that the next READ will continue picking information off of the current record, or the next WRITE will write to the same record (line on the screen) starting at the position where the last READ or WRITE stopped. To use this option you must have a specific format, either labeled FORMAT statement, or format specifications in a character string (variable or constant). This will not work with a list-directed (*) format.REC=
Is used for Direct Access Files only, specifying the record number to be read or written. The equals can be followed by an integer constant, but usually is followed by an integer variable containing the value of the record number to be read.The statement
read(11,1001, err=700,iostat=ioerr,rec=93) x,y,zwill read values for x, y, and z from record number 93 of a direct access file assigned to unit 11, according to FORMAT number 1001 (this is unusual since direct access files are normally unformatted). If an error occurs, the program will go to label 700, where the value of variable "ioerr" could be printed, or acted on with logic specific to the current computer.
If your READ statement tries to input more numbers than exist on the input line, this option provides a label to which execution will branch for corrective action. This may only be used for READs containing the ADVANCE='NO' option.
read(12, 1001, end=600) x,y,zreads variables x, y, and z from the sequential file attached to unit 12, according to FORMAT with label "1001". If an End of File is encountered, the program puts no values into x, y, or z, and instead branches to label 600 to continue execution.
If for some reason you get bored with the above form of READ (or analogous WRITE), you can be more specific about argument meaning.
read(unit=12, fmt=1001, end=600) x,y,z
writes binary image of the contents of x, y, and z to unit 12.
Followed with a quoted string or character variable containing the name of the file to be opened.STATUS=
Followed by one of several possible quoted character strings to specify the status of the file. For STATUS='OLD', an error condition is flagged (branch to any ERR=label occurs) if the file does not already exist. For STATUS='NEW' and error condition is flagged if the file already exists. This is great to keep yourself from clobbering existing data. STATUS='SCRATCH' creates a file that will automatically be deleted when you close the unit. No file name (FILE=) may be used here. If you don't include the STATUS option, Fortran uses the default STATUS='UNKNOWN', and will create a new file if needed, or connect to an existing one. Fortran 90 adds the STATUS='REPLACE' option which first deletes any file with the same name before opening a new file.ACCESS=
This how you specify sequential (access='sequential') or direct access (access='direct'). The only normal use for this is direct access since the default for OPEN is sequential.RECL=
If you declare direct access, you must specify the fixed length of records (e.g. recl=100).FORM=
Declare that the file contains either 'FORMATTED' or 'UNFORMATTED' data. The default for sequential files is formatted. The default for direct access is unformatted.POSITION= (Fortran 90 only)
Tells where you are positioned in the file when you open it. Use POSITION='APPEND', to append data to the end of an existing file.
inquire(file='test.file',exist=lexist)will result in a value of .true. in the logical variable 'lexist' if 'test.file' exists in the current directory. You can check if the file has been opened with:
inquire(file='test.file',opened=lopen) .where 'lopen' is a logical variable, or if a unit is connected with something like:
inquire(unit=11,opened=lopen) .For files that are opened inquire will also let you check the details of any option set by the open command (ACCESS=, RECL=, ...) by placing an appropriate (CHARACTER, or INTEGER) variable after the "=" to receive the information.
Check your knowledge of this material, but first be sure your Web Browser works correctly.
Written and Maintained by John Mahaffy : email@example.com