[comp.os.vms] FORTRAN&C datafile compatibility

SCHOMAKE@HNYKUN53.BITNET (Lambert Schomaker) (09/22/87)

[]
The following file description is from a FORTRAN-produced data file
(sequential, unformatted, variable length records). We would like to
read and create such data files with programs written in C.

IDENT  "VAX/VMS ANALYZE/RMS_FILE Utility"
SYSTEM
    SOURCE                  VAX/VMS
FILE
    ALLOCATION              11
    BEST_TRY_CONTIGUOUS     no
    CLUSTER_SIZE            1
    CONTIGUOUS              no
    EXTENSION               0
    GLOBAL_BUFFER_COUNT     0
    NAME                    "DUA0:foo.foo"
    ORGANIZATION            sequential
RECORD
    BLOCK_SPAN              yes
    CARRIAGE_CONTROL        none
    FORMAT                  variable
    SIZE                    0

Does anybody know what fopen tricks are necessary? What function should we
use for reading/writing the records? Thanks in advance,
                 *
               ^^^^^
      KKKKKUUUUNNNNN
      KKK  UUUU NNNN           Lambert Schomaker
      K    UUUU  NNN           SCHOMAKE@HNYKUN53.BITNET
      KKK  UUUU   NN           Nijmegen, The Netherlands.
      KKKKK UU     N

leichter@VENUS.YCC.YALE.EDU ("Jerry Leichter") (09/28/87)

	The following file description is from a FORTRAN-produced data file
	(sequential, unformatted, variable length records). We would like to
	read and create such data files with programs written in C.

	IDENT  "VAX/VMS ANALYZE/RMS_FILE Utility"
	SYSTEM
	    SOURCE                  VAX/VMS
	FILE
	    ALLOCATION              11
	    BEST_TRY_CONTIGUOUS     no
	    CLUSTER_SIZE            1
	    CONTIGUOUS              no
	    EXTENSION               0
	    GLOBAL_BUFFER_COUNT     0
	    NAME                    "DUA0:foo.foo"
	    ORGANIZATION            sequential
	RECORD
	    BLOCK_SPAN              yes
	    CARRIAGE_CONTROL        none
	    FORMAT                  variable
	    SIZE                    0

	Does anybody know what fopen tricks are necessary? What function
	should we use for reading/writing the records?

There are two levels of problem here.  First off, for every language EXCEPT
FORTRAN, an FDL description will normally supply all the information needed
to determine the exact format of the file.  FORTRAN, however, implements
SEGMENTED files.  "SEGMENTED" is not an RMS file type, and you cannot tell
if a file is a SEGMENTED file from its RMS characteristics - a SEGMENTED file
is to RMS a sequential, variable-length-record, file - and, in fact, if you
create or access such a file, FORTRAN by default assumes it is a SEGMENTED
file.  But a SEGMENTED file has a structure, supported only by the FORTRAN
I/O system, to support very large FORTRAN records that consist of multiple
RMS records.  The (rather simple) details are given in the FORTRAN documen-
tation - as I recall, the first two bytes of each RMS record contain a flag
indicating one of three states:  Beginning of FORTRAN record; continuation of
FORTRAN record; end of FORTRAN record.  If your files are, indeed, SEGMENTED,
you will have to write your own C code to interpret or create the same
structures the FORTRAN I/O system uses.  (You could also write some FORTRAN
subroutines to do the I/O for the C code.)

Given all that, the RMS structures are easy to replicate in C:  Use

	fopen(file,"w","rfm=var");

to create such a file.  "Carriage control=none" and "no block span" are the
defaults.  If you really care about the ALLOCATION, use:

	fopen(file,"w","rfm=var","alq=11");

(Normally, space is allocated to the file as required.  Pre-allocating the
space can be worthwhile for large files, but for an 11-block file, unless
something unusual is going on, you'll never notice the difference.)

You need no options to open the file for input - just use fopen() directly.

fread() and fwrite() should handle the records in such a file quite nicely.
Alternatively, you can open the file with open() rather than fopen(), then
use read() and write().  This will be a little more efficient, but probably
not enough to be noticed.
							-- Jerry
------