⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fio.gml

📁 开放源码的编译器open watcom 1.6.0版的源代码
💻 GML
📖 第 1 页 / 共 2 页
字号:
.chap *refid=fio Input/Output
.*
.if &e'&dohelp eq 0 .do begin
.section Introduction
.do end
.*
.np
FORTRAN 77 provides a means of communicating information or
.us data
.ix data
between a FORTRAN program and the computing environment.
The computing environment may include a number of devices which are
capable of the recording, retrieval, display, and input of data.
Disk and magnetic tape devices are capable of storing large amounts
of data.
Other devices such as printers and display terminals can be used to
present a visual (i.e., human-readable) representation of the data.
Yet other devices such as terminal keyboards and card-readers make
possible the entry of new data into the computing environment.
.np
For the purposes of our discussion, data is any information which
can be processed by an executing FORTRAN program.
Some examples of data are names, addresses, telephone numbers,
credit card balances, flight trajectories, bus schedules, athletic
records, etc.
In computing, such information is usually well-organized in order to
make it useful for processing.
.np
To use an example, consider the entries in a telephone book.
There are essentially three pieces of data listed for each entry; a
name, an address, and a number.
.millust begin
    Smith J 32 Arthur St--------------------------555-3208
    Smith JW 512 King St--------------------------555-9229
    Smith Jack 255-113 Queen St N-----------------555-0572
.millust end
.pc
Each entry is a
.us record.
The organization of the book is clear.
The name is always listed first, the address second, and the number
last.
The records are
.us sorted,
for our convenience, by name (within each city or geographical
location).
The length of each record is the same.
This
.us fixed length
.ix 'record' 'fixed length'
does sometimes lead to problems since entries which have a long
name or address won't fit in a record.
The phone company solved this by continuing the information in
subsequent records.
We might have solved this problem by increasing the length of a
record with the disadvantage of wasting a lot of printing space.
Alternatively, we could have used a
.us variable length
.ix 'record' 'variable length'
record.
This solves the problem of wasted space but creates a severe problem
when trying to display the records in nice orderly columns.
The telephone book itself is a collection of records or a
.us file.
.ix file
.np
We have introduced much of the terminology of data processing:
"data", "records", "fixed and variable record sizes", "files",
"sorted", etc.
.*
.section Reading and Writing
.*
.np
FORTRAN provides a mechanism called "reading" for transferring
data into the environment of an executing program.
The
.kw READ
statement is used to do this.
Similarly "writing" is the mechanism for transferring data out of
an executing program.
The
.kw WRITE
and
.kw PRINT
statements are used to do this.
Other statements provide additional functions such as positioning to
a certain record in a file, establishing which files are to be
processed by the program, or making inquiries about files.
.*
.section Records
.*
.np
.ix record
FORTRAN distinguishes between three kinds of records, namely:
.autopoint
.point
Formatted
.point
Unformatted
.point
Endfile
.endpoint
.np
We shall describe each of these in the following sections.
.*
.beglevel
.*
.section Formatted Record
.*
.np
.ix 'formatted record'
.ix record formatted
A formatted record consists of characters.
The length of a formatted record is determined by the number of
characters in it.
A formatted record may contain no characters at all and thus has
zero length.
.ix 'input/output' 'formatted'
.ix 'formatted input/output'
Formatted records are read or written using
.us formatted input/output
statements.
An excellent example of a file consisting of formatted records is
our telephone book example.
.*
.section Unformatted Record
.*
.np
.ix 'unformatted record'
.ix record unformatted
An unformatted record consists of values such as integers, real
numbers, complex numbers, etc.
It may also consist of characters.
Essentially, these values have the same representation in a record
as they have in the computer's memory.
The length of an unformatted record depends on the amount of storage
required to represent these values in the computer's memory.
For example, on this computer an integer value is stored using 4
bytes of memory (a byte is a grouping of 8 binary digits).
Thus, integer values in unformatted records also require 4 bytes of
storage.
For example, 3 integer values stored in an unformatted record would
require 12 bytes of storage.
.ix 'input/output' 'unformatted'
.ix 'unformatted input/output'
Unformatted records are read or written using
.us unformatted input/output
statements.
.np
To illustrate the difference between a formatted and unformatted record
consider the following example.
.exam begin
      INTEGER NUMBER
      NUMBER=12345
      PRINT 100, NUMBER
100   FORMAT(1X,I5)
      WRITE( UNIT=7 ) NUMBER
.exam end
.pc
If you print the variable
.id NUMBER
on a printer, it requires 5 character positions.
If you write it to a file using an unformatted
.kw WRITE
statement, it only requires 4 bytes or character positions in the
record.
Note that a character is conveniently represented in one byte of
storage, hence we sometimes use the term "byte" or "character"
interchangeably when talking about the size of variables.
.*
.section Endfile Record
.*
.np
.ix 'endfile record'
.ix record endfile
An endfile record is a special record that follows all other records
in a file.
Simply stated, an endfile record occurs at the end of a file.
Actually, an endfile record is a conceptual thing.
It has no length.
When the end of a file is reached (i.e., an attempt to read a record
results in the endfile record being read), an "end-of-file"
condition exists.
.ix end-of-file
There are no more records following the endfile record.
There is only one endfile record so it is strictly illegal to
attempt to read another record after the endfile record has been
read (i.e., when the end-of-file condition exists).
.endlevel
.*
.section Files
.*
.np
.ix file
Earlier we described the notion of a file as a collection of records.
In FORTRAN, there are two kinds of files:
.autopoint
.point
External
.point
Internal
.endpoint
.*
.beglevel
.*
.section External Files
.*
.np
.ix file external
.ix 'external file'
.ix 'file existence'
External files are files that exist or can be created upon external
media such as disks, printers, terminal displays, etc.
A file may exist before the execution of a FORTRAN program.
It may be brought into existence or "created" during execution.
It may also be deleted and therefore not exist after the execution
of a FORTRAN program.
.np
All input/output statements may refer to files that exist.
In addition, the
.kw INQUIRE,
.kw OPEN,
.kw CLOSE,
.kw WRITE,
.kw PRINT,
and
.kw ENDFILE
statements may refer to files that do not exist (and in so doing, may
very well cause the file to be created).
.begnote Properties of External Files
.ix 'external file' properties
.note Name
.ix 'external file' name
In FORTRAN, a file may or may not have a name.
If it does have a name then, not surprisingly, it is called a
.us named
file.
.ix file name
All files in &product have names and so it may seem odd to
introduce this notion.
However, we do since the
.kw INQUIRE
statement lets you find out if a file is named and, if so, what its
name is.
File naming conventions may differ from one computing system to the
next.
As well, different FORTRAN 77 compilers may have different file
naming conventions.
.note Access
.ix access
.ix 'external file' access
"Access" simply refers to the way in which we can position to and read
or write the data in a particular record in a file.
There are two ways in which records can be accessed in a file;
.us sequentially
or
.us directly.
.np
.ix access sequential
.ix 'sequential access'
Using the
.us sequential access
method, records may be read or written in
order starting with the first record and proceeding to the last record.
For example, it would be quite impossible to read or write the tenth
record in a file and then read or write the third record.
Similarly the eleventh record must be read or written before we can
access the twelfth record.
If we adopt the convention that each record in a file has a record
number then the first record is record number 1, the second is 2,
and so on.
This numbering convention is important when we look at the other
access method which is "direct".
.np
.ix access direct
.ix 'direct access'
Using the
.us direct access
method, records may be read or written in any
order.
It is possible to read or write the tenth record of a file and then
the third and then the twelfth and so on.
A caveat: a record cannot be read if it has never been written since
the file was created.
In direct access, the idea of a record number is very important and
so by convention, we number them starting at 1 as the first record
and proceeding on up.
With direct access, if you create a new file and write record number
10 then the file has ten records regardless of the fact that only
one has been written.
You could, at some later time, write records 1 through 9 (in
whatever order you please) and add additional records by writing
records with record numbers greater than 10.
.np
Some files have the property of being able to sustain both of these
access methods.
Some files may only have one of these properties.
For example, most line printers cannot be accessed directly.
You have no choice but to write records sequentially.
Sometimes a file that was created using the sequential access method
may not be accessed using the direct method or vice versa.
FORTRAN calls this property of a file the "set of allowed access
methods".
.note Record Form
.ix record form
.ix 'external file' 'record form'
Some files have the property of being able to handle both formatted
and unformatted record formats.
Some files may only have one of these properties.
For example, if you tried to write unformatted records to a line
printer, the result might be gibberish.
On the other hand a graphics printer may readily accept unformatted
records for reproducing graphical images on paper.
FORTRAN calls this property of a file the "set of allowed forms".
.note Record Length
.ix record length
.ix 'external file' 'record length'
Another property of a file is record length.
Some files may have restrictions on the length of a record.
Some files do not allow records of zero length.
Other files, such as printers, may restrict the length of a record
to some maximum.
FORTRAN calls this property the "set of allowed record lengths".
.endnote
.*
.section Internal Files
.*
.np
.ix file internal
.ix 'internal file'
Internal files are special files that reside only in memory.
They do not exist before or after the execution of a FORTRAN
program, only during the execution of a program.
An internal file allows you to treat memory in the computer as if it
were one or more records in a file.
The file must be a character variable, character array element,
character array, or character substring.
A record in this file may be a character variable, character array
element or character substring.
.np
Another way of looking at this is that an internal file that is either
a character variable, character array element or character substring
can contain only one record but an internal file that is a character
array can contain several records (as many as there are elements in the
array).
.begnote Properties of Internal Files
.ix 'internal file' properties
.note Records
.ix 'internal file' records
Unless the name of a character array is used, only one record is
contained in an internal file.
The length of this record is the same as the length of the variable,
array element, or substring.
If the file is a character array then each element in the array is a
record.
The order of the records in the file is the same as the order of the
elements in the array.
The length of a record in this case is the same as the length of the
character array elements.
.np
If the number of characters written to a record in an internal file
is less than the length of the record then the record is padded with
blanks.
.note Definition
.ix 'internal file' definition
A record may be read only if the variable, array element, or substring
is defined (i.e., it has been assigned some value).
Definition may not only result from an output statement such as
.kw WRITE.
It may also be defined through other means; for example, a character
assignment statement.
.note Position
.ix 'internal file' position
For all input/output statements, the file is positioned at the
beginning of the first record.
Multiple records may be read or written using the "slash" format edit
descriptor (see the chapter entitled :HDREF refid='fformat'.).
.note Restrictions
.ix 'internal file' restrictions
Only sequential access formatted input and output statements (
.ct
.kw READ
and
.kw WRITE
.ct ) may be used to transfer data to and from records in an
internal file.
.np
.xt begin
.ix list-directed
Although FORTRAN 77 states that list-directed formatted input/output
to an internal file is not permitted, &product allows you to use
list-directed formatted input/output statements.
This is an extension to the language standard.
.exam begin
      WRITE(INTFIL,*) X, Y, Z
.exam end
.xt end
.np
No other input/output statements (
.ct
.kw OPEN,
.kw ENDFILE,
.kw REWIND,
etc.) may be used.
.endnote
.np
Internal files may be used to convert data from one format to another.
The following example illustrates one use of internal files.
.cp 15
.exam begin
      CHARACTER*11 INPUT
      PRINT *, 'TYPE IN ''I'' FOLLOWED BY AN INTEGER'
      PRINT *, 'OR TYPE IN ''R'' FOLLOWED BY A REAL'
      READ 100, INPUT
100   FORMAT( A11 )
      IF( INPUT(1:1) .EQ. 'I' )THEN
          READ( UNIT=INPUT(2:11), FMT='(I10)' ) IVAR
          PRINT *, 'AN INTEGER WAS ENTERED ', IVAR
      ELSE IF( INPUT(1:1) .EQ. 'R' )THEN
          READ( UNIT=INPUT(2:11), FMT='(F10.3)' ) RVAR
          PRINT *, 'A REAL NUMBER WAS ENTERED ', RVAR
      END IF

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -