Introduction to Input/Output [ SPL to HP C/XL Migration Guide ] MPE/iX 5.0 Documentation
SPL to HP C/XL Migration Guide
Introduction to Input/Output
SPL has no input/output (I/O) statements; instead, it uses MPE V
intrinsics to perform all I/O operations.
Similarly, HP C/XL has no I/O statements; it does have its own library
header file, <stdio.h>, that provides a comprehensive set of macros and
functions for I/O capabilities, including high level formatting. HP C/XL
also has a special library header file, <mpe.h>, that provides an
interface to the MPE XL I/O intrinsic library. This arrangement allows
HP C/XL programmers to choose either HP C/XL I/O functions and macros,
MPE XL I/O intrinsics, or a combination of both.
In general, the MPE XL I/O intrinsics are identical to or extensions of
the MPE V versions. The differences are described in the Introduction to
MPE XL for MPE V Programmers migration guide. Consult the MPE XL
Intrinsics Reference Manual| for the complete specification of all MPE XL
intrinsics.
There are strong arguments in favor of adopting the HP C/XL style of I/O
operations. Programmer convenience and program portability are high on
the list. Programs that use HP C/XL library functions can usually be
transferred to HP C/HP-UX with little or no modification. The source
code changes that are required anyway to provide parameters to the MPE XL
intrinsics can just as easily be revised to use HP C/XL library functions
instead.
It is recommended that SPL programs being translated into HP C/XL adopt
as many of the HP C/XL I/O facilities as possible. Where there are
necessary operations that cannot be performed by the HP C/XL standard
library header file, <stdio.h>, MPE XL intrinsics may be declared with
the #pragma intrinsic directive and called directly.
CAUTION You cannot use the HP C/XL I/O system and another I/O system
concurrently to write data to the same disk file (except for the
stdout and stderr file streams). Please consult the HP C/XL
Library Reference Manual for details.
Example
Since all I/O operations by MPE V intrinsics use 16-bit data, it is
common to equivalence a BYTE array to a previously declared LOGICAL word
array. Then data is stored into or extracted from the byte array, while
the equivalent word array is passed to the MPE V intrinsics.
As an example of the convenience of the HP C/XL constructs, consider the
following SPL program fragment and the identical operation in HP C/XL:
---------------------------------------------------------------------------------------------
| | |
| SPL | HP C/XL Equivalent |
| | |
---------------------------------------------------------------------------------------------
| | |
| | |
| LOGICAL ARRAY BUFW(0:40); | short int X; |
| <<equate byte to word array>> | printf("value of X = %d\n",X); |
| BYTE ARRAY BUF(*)=BUFW; | |
| INTEGER X; | |
| INTRINSIC PRINT, ASCII; | |
| <<move 18 bytes>> | |
| MOVE BUF:="value of X = "; | |
| <<convert to ASCII>> | |
| ASCII(X,10,BUF(13)); | |
| <<output word array copy>> | |
| PRINT(BUFW,9,0); | |
| | |
---------------------------------------------------------------------------------------------
Record Format
The "normal" SPL file has fixed-length records, although files with
variable length records can be created and used. The "normal" HP C/XL
file, called a "stream", has variable-length records; files with
fixed-length records can be created and used.
File References
There are three distinct variables that specify a file, depending on
which HP C/XL function or MPE intrinsic opened it. These variables are
used to identify the file access to other functions or intrinsics. The
HP C/XL function open returns filedes, an int file descriptor; the HP
C/XL function fopen returns stream, a pointer to type FILE; and the MPE
intrinsic FOPEN returns filenum, a 16-bit integer file number. The MPE
XL intrinsic HPFOPEN also returns filenum, but as a 32-bit integer file
number, equal to the FOPEN value.
Fortunately, there is a relationship among them. A stream file pointer
can be obtained from a fildes file descriptor with the HP C/XL <stdio.h>
library function fdopen:
#include <stdio.h>
stream = fdopen(fildes)
An MPE filenum can be obtained from fildes with the HP C/XL <mpe.h>
library function _mpe_fileno:
#include <mpe.h>
filenum = _mpe_fileno(filedes)
See the HP C/XL Library Reference Manual for more details.
Conflicting Function and Intrinsic Identifiers
Five of the functions in the HP C/XL standard library have the same names
as MPE intrinsics: fopen, fclose, fread, fwrite, and read. If any of
the MPE intrinsics of the same name are used, it is recommended that you
rename them with the #pragma intrinsic directive to avoid confusion. For
instance:
#pragma intrinsic FREAD MPE_FREAD
Although case sensitivity would render FREAD distinct from fread, the use
of MPE_FREAD is much more descriptive. It's probably a good idea to
apply the same renaming scheme to all the MPE intrinsics your program
uses, just to make them easier to find.
Error Reporting
The MPE intrinsics vary in how errors are reported. Some return an error
value for a function value or parameter, but most have a side effect of
setting the condition code. The HP C/XL library function ccode returns
the most recent setting of the condition code.
The HP C/XL I/O functions report an error by returning an error value,
and sometimes by setting an external variable errno. The value of errno
will indicate the error which caused the most recent intrinsic or library
function error. Its value is not changed or reset until the next
instance of an error, so errno should not be interrogated unless a
function that sets it reports an error.
Summary of Intrinsics, Macros, and Functions
Table 9-1 lists the MPE XL I/O intrinsics. Note that all but HPFOPEN are
equivalent to the MPE V versions. HPFOPEN is only available in MPE XL.
It has clearer ways of passing parameters than FOPEN, as well as having
more options. See the MPE XL Intrinsics Reference Manual for details.
Table 9-1. MPE XL I/O Intrinsics
-------------------------------------------------------------------------------------------
| Intrinsic Description |
-------------------------------------------------------------------------------------------
| FCHECK(filenum,fserr,translog,block,nrec) Get details on I/O errors |
| FCLOSE(filenum,disp,seccode) Close file |
| FCONTROL(filenum,controlcode,param) Perform control operation on file or |
| terminal |
| FOPEN(formdesig,foptions,aoptions,...) Open file; return filenum, 16-bit file |
| number |
| FREAD(filenum,buffer,length) Read logical record from sequential file; |
| return count |
| FREADDIR(filenum,buffer,length,lrecnum) Read logical record from direct access file |
| FSPACE(filenum,disp) Space forward or backward on file |
-------------------------------------------------------------------------------------------
| FUPDATE(filenum,buffer,length) Update logical record in file |
| FWRITE(filenum,buffer,length,ctlcode) Write logical record to sequential file |
| FWRITEDIR(filenum,buffer,length,lrecnum) Write logical record to direct access file |
| HPFOPEN(filenum,status[,itemnum,item][...]) Open file; return filenum, 32-bit file |
| number |
| PRINT(message,length,ctlcode) Write string to $STDLIST |
| READ(message,expectedlength) Read string from $STDIN; return actual |
| length |
| READX(message,expectedlength) Read string from $STDINX; return actual |
| length |
-------------------------------------------------------------------------------------------
Table 9-2 and Table 9-3 describe briefly the HP C/XL standard library I/O
macros and functions that you may wish to use in converting your SPL
programs. See the HP C/XL Library Reference Manual for details.
Table 9-2. HP C/XL I/O Macros
-------------------------------------------------------------------------------------------
| Macro Description |
-------------------------------------------------------------------------------------------
| getc(stream) Read one character from file stream |
| getchar() Read one character from stdin |
| putc(c,stream) Write one character c to file stream |
| putchar(c) Write one character c to stdout |
-------------------------------------------------------------------------------------------
Table 9-3. HP C/XL I/O Functions
-------------------------------------------------------------------------------------------
| Function Description |
-------------------------------------------------------------------------------------------
| access(filename,access) Test accessibility of file |
| clearerr(stream) Clear error and eof conditions on file |
| stream |
| close(fildes) Close file fildes |
| dup(fildes) Duplicate file descriptor fildes |
| fclose(stream) Close file stream; flush buffer |
| fdopen(fildes) Get stream pointer from fildes file |
| descriptor |
| feof(stream) Test file stream for end-of-file |
-------------------------------------------------------------------------------------------
| ferror(stream) Test file stream for error |
| fflush(stream) Flush buffer to file stream |
| fgetc(stream) Read one character from file stream |
| fgets(string,n,stream) Read n-1 chars from file stream (or up to |
| '\n') |
| fopen(filename,type) Open file filename; return stream (pointer |
| to FILE) |
| fprintf(stream,format[,item][...]) Convert from internal item; write to file |
| stream |
| fputc(c,stream) Write one character c to file stream |
-------------------------------------------------------------------------------------------
| fputs(string,stream) Write string (up to '\0') to file stream |
| fread(ptr,size,nitems,stream) Read fixed-length binary records from file |
| stream |
| freopen(filename,type,stream) Change file attached to stream |
| fscanf(stream,format[,item][...]) Read from stream; convert to internal item |
| fseek(stream,offset,ptrname) Set byte position in file stream |
| ftell(stream) Return byte position of file stream |
| fwrite(ptr,size,nitems,stream) Write fixed-length binary records to file |
| stream |
-------------------------------------------------------------------------------------------
| gets(string) Read string from stdin |
| getw(stream) Read int word from file stream |
| lseek(fildes,offset,ptrname) Set byte position in file fildes |
| open(filename,oflag,mode,mpeopts) Open file filename; return fildes (int file |
| descriptor) |
| printf(format[,item][...]) Convert from internal item; write to stdout |
| puts(string) Write string (up to '\0') to stdout |
| putw(word,stream) Write int word to file stream |
-------------------------------------------------------------------------------------------
| read(fildes,buf,nbyte) Read fixed-length binary records from file |
| fildes |
| remove(filename) Purge file filename |
| rename(oldname,newname) Rename file |
| rewind(stream) Reset byte position to beginning of file |
| stream |
| scanf(format[,item][...]) Read from stdin; convert to internal item |
| setbuf(stream,buffer) Define buffer for file stream |
| setvbuf(stream,buffer,type,size) Define buffer for file stream |
-------------------------------------------------------------------------------------------
| sprintf(string,format[,item][...]) Convert from internal item; write to string |
| sscanf(string,format[,item][...]) Read from string; convert to internal item |
| tmpfile() Open unnamed tempfile |
| tmpnam(string) Create temp filename in string |
| ungetc(c,stream) Push back character c to input file stream |
| unlink(filename) Purge file filename |
| write(fildes,buf,nbyte) Write fixed-length binary records to file |
| fildes |
-------------------------------------------------------------------------------------------
MPE/iX 5.0 Documentation