|
|
IRIX 6.5 » Books » Developer »
MIPSpro Fortran 90 Programmer's I/O Guide
(document number: 007-3695-006 / published: 2002-11-19)
table of contents | additional info | download find in page
Chapter 5. System and C I/O
This chapter describes systems calls used by the I/O library to perform
asynchronous or synchronous I/O. This chapter also describes Fortran callable
entry points to several C library routines.
The I/O library
and programs use the system calls described in this chapter to perform synchronous
and asynchronous I/O, to queue a list of distinct I/O requests, and to perform
unbuffered I/O without system buffering.
With synchronous I/O, an executing program relinquishes control
during the I/O operation until the operation is complete. An operation is
not complete until all data is moved.
The read(2) and write(2)
system calls perform synchronous reads and writes. The
READ(3f) and WRITE(3f) functions provide
a Fortran interface to the read and write
system calls. The read system call reads a specified number
of bytes from a file into a specified buffer. The write
system call writes from a buffer to a file.
Asynchronous I/O lets the program use the time that
an I/O operation is in progress to perform some other operations that do not
involve the data in the I/O operation. In asynchronous I/O operations, control
is returned to the calling program after the I/O is initiated. The program
may perform calculations unrelated to the previous I/O request or it may issue
another unrelated I/O request while waiting for the first I/O request to complete.
The asynchronous I/O routines provide functions that let a program wait
for a particular I/O request to complete. The asynchronous form of
BUFFER IN and BUFFER OUT statements used with
UNIT and LENGTH routines provide this type of
I/O.
The open(2) system call opens
a file for reading or writing. If the I/O request is well-formed and the
O_RAW flag is set, the read(3f) or write(3f) system call reads or writes whole blocks of
data directly into user space, bypassing system cache.
The C library provides a set of routines that constitute a user-level
I/O buffering scheme to be used by C programmers.
The getc(3c) and
putc(3c) inline macros process characters. The getchar
and putchar macros, and the higher-level routines
fgetc, fgets, fprintf,
fputc, fputs, fread,
fscanf, fwrite, gets,
getw, printf, puts,
putw, and scanf all use or act as if they use
getc and putc. They can be intermixed.
A file with this associated buffering is called a streams and is associated with a pointer
to a defined type FILE. The fopen
(3c) routine creates descriptive data for a stream and returns a pointer
to designate the stream in all further transactions. Three open streams with
constant pointers are usually declared in the <stdio.h>
header file and are associated with stdin, stdout
, and stderr.
Three types of buffering are available
with functions that use the FILE type: unbuffered, fully
buffered, and line buffered, as described in the following list: If the stream is unbuffered, no library buffer is used.
For a fully buffered stream, data is written from the library
buffer when it is filled, and read into the library buffer when it is empty.
If the stream is line buffered, the buffer is flushed when
a new line character is written, the buffer is full, or when input is requested.
The setbuf
and setvbuf functions let you change the type and size
of the buffers. By default, output to a terminal is line buffered, output
to stderr is unbuffered, and all other I/O is fully buffered.
See the setbuf(3c) man page for details.
Mixing the use of C I/O functions with Fortran I/O
on the same file may have unexpected results. If you want to do this, ensure
that the Fortran file structure chosen does not introduce unexpected control
words and that library buffers are flushed properly before switching between
types of I/O.
The following example illustrates the
use of some C routines. The assign environment does not
affect these routines.
Example 5-1. C I/O from Fortran
PROGRAM STDIOEX
INTEGER FOPEN, FCLOSE, FWRITE, FSEEK
INTEGER FREAD, STRM
CHARACTER*25 BUFWR, BUFRD
PARAMETER(NCHAR=25)
C Open the file /tmp/mydir/myfile for update
STRM = FOPEN('/tmp/mydir/myfile','r+')
IF (STRM.EQ.0) THEN
STOP 'ERROR OPENING THE FILE'
ENDIF
C Write
I = FWRITE(BUFWR, 1, NCHAR, STRM)
IF (I.NE.NCHAR*1)THEN
STOP 'ERROR WRITING FILE'
ENDIF
C Rewind and read the data
I = FSEEK(STRM, 0, 0)
IF (I.NE.0)THEN
STOP 'ERROR REWINDING FILE'
ENDIF
I = FREAD(BUFRD, 1, NCHAR, STRM)
IF (I.NE.NCHAR*1)THEN
STOP 'ERROR READING FILE'
ENDIF
C Close the file
I = FCLOSE(STRM)
IF (I.NE.0) THEN
STOP 'ERROR CLOSING THE FILE'
ENDIF
END |
MIPSpro Fortran 90 Programmer's I/O Guide
(document number: 007-3695-006 / published: 2002-11-19)
table of contents | additional info | download
Front Matter
About This Guide
Chapter 1. Introduction
Chapter 2. Standard Fortran I/O
Chapter 3. Fortran I/O Extensions
Chapter 4. Named Pipe Support
Chapter 5. System and C I/O
Chapter 6. The assign Environment
Chapter 7. File Structures
Chapter 8. Buffering
Chapter 9. Introduction to FFIO
Chapter 10. Using FFIO
Chapter 11. Foreign File Conversion
Chapter 12. I/O Optimization
Chapter 13. FFIO Layer Reference
Chapter 14. Creating a user Layer
Glossary
Index
home/search |
what's new |
help
|
|
|