2. File Processing
Applications involving large data sets can be
processed more conveniently if the data is
stored in a file.
The data in the file to be used as an input file
must be arranged in a form suitable for
reading by a read statement.
These values are read during program
execution, just like data entered by the user
from the keyboard.
3. Opening Files
form OPEN ( open_list )
opens the file associated with specified
unit number so that Input/Output can
take place.
4. Open List
Open_list includes:
1) a unit specifier indicating a unit
number connected to the file being
opened. The unit specifier has the form
described for the write statement.
2) FILE = Character_expression giving
the name of the file being opened.
5. Open List
3) Status = character_expression where
character expression is one of the following
“old”,”new”, or “replace”.
*Old means the file already exists.
*New means the file does not yet exist and is
being created.
*Replace creates a new file, replacing the old
one if one already exists, and changes its
status to old
6. Open List
Action = i-o-action
where i-o-action is a character expression
whose value is one of the following:
“Read”: file opened for reading only
“Write”: file opened for writing only
“Readwrite”: file opened for both reading and
writing.
7. Open List
Position = character_expression
where the character expression is on of
“Rewind”: position the file at its initial point.
“Append”: position the file at the end of file.
“ASIS”: leaves the position unchanged..
8. Open List
IOSTAT = Status_variable
where status variable is an integer
variable to which the value zero is
assigned if the file is opened
successfully and a positive value is
assigned otherwise.
9. Ex:
To open a file named info.dat from which data
values are to be read:
open(unit=12,FILE=“info.dat”,status=“old”,&
action=“read”, Position=“rewind”,&
IOSTAT = OpenStauts)
where openstatus is an integer variable equal to zero
if the file is successfully opened or some positive
value if the file cannot be open.
10.
If the program is to used with various data
files, it’s better not to assign the file name
directly but to use a character variable.
Character(12):: filename
Write(*,’(1X,A)’,ADVANCE=“NO”) &
“Enter name of data file”
read *, filename
open(unit=12,FILE=filename,status=“old”,&
action=“read”, Position=“rewind”,&
IOSTAT = OpenStauts)
11.
To open a new file named Report to
which values are to be written;
open(unit=13,File=“Report”,status=“new”,&
action=“write”, IOSTAT= openstatus)
To write values to the file, we use the write
statement
Write(13,’(1X,I3)’) Code
12. Closing Files
The CLOSE statement has a function
opposite that of the OPEN statement
and is used to disconnect a file from its
unit number.
Form
close (unit_number)
13. Notes
After a close statement is executed, the
closed file may be reopened by using an
open statement; the same unit number may
be connected to it or a different on may be
used.
All files that are not explicitly closed by means
of a close statement are automatically closed
when and End or Stop statement is executed.
14. File Input/Output
Once a file has been connected to a
unit number, data can be written to or
form that file using the general WRITE
and READ statements.
A general read statement may contain
an IOSTAT= clause to detect an end-offile condition or an input error.
15.
IOSTAT = status_variable,
Status_variable is assigned
A positive value if an input error occurs.
A negative value if the end of data is
encountered but no input errors occurs.
Zero if neither an input error not the end of
data occurs.
17.
An alternative method of detecting an
end-of-file condition is to use an
END=clause in the control list of a
general read statement.
Form END= statement_number.
19. File Positioning Statements
Rewind Statement:
Rewind unit_number
positions the file at the beginning.
Backspace Statement:
Backspace unit_number
positions the file at the beginning of the
preceding line.