Example EAF Synchronous Program
This is a small example of an EAF program which uses synchronous I/O to open, write to, then read back from a file.
#include "chemio.h" #include "eaf.fh" integer fh ! File Handle integer sz ! Return value of size written integer stat ! Return status integer buf(100) ! Data to write fh = EAF_OpenPersist('/tmp/test.out', CHEMIO_RW) sz = EAF_Write(fh, 0, buf, 100*EAF_SZ_INT)
if(sz .ne. 100*EAF_SZ_INT) $ write(0,*) 'Error writing, wrote ', sz, ' bytes' sz = EAF_Read(fh, 0, buf, 100*EAF_SZ_INT) if(sz .ne. 100*EAF_SZ_INT) $ write(0,*) 'Error reading, read ', sz, ' bytes' stat = EAF_Close(fh) end
The include file 'chemio.h' defines the permission macros CHEMIO_R, CHEMIO_W, and CHEMIO_RW for read, write, and read-write permissions, respectively. The header file 'eaf.fh' is a Fortran program segment externally defining the EAF routines and must appear before any executable code using EAF.
EAF_OpenPersist opens a persistent file, as opposed to a scratch file (EAF_OpenScratch) which is deleted when it is closed. This file is named '/tmp/test.out' and has read-write permissions. The returned value is the file handle for this file and should not be directly manipulated by the user.
EAF_Write writes to the file opened with file handle, fh, at absolute offset 0. It is legal to write a scalar or array, for instance in the above example both 'buf' and 'buf(1)' have the same meaning. The last argument is the number of bytes to be written. It is important to multiply the number of array elements by the element size. The following macros are provided in 'eaf.fh':
EAF_SZ BYTE
EAF_SZ_CHARACTER
EAF_SZ_INTEGER
EAF_SZ_LOGICAL
EAF_SZ_REAL
EAF_SZ_COMPLEX
EAF_SZ_DOUBLE_COMPLEX
EAF_SZ_DOUBLE_PRECISION
The return value is the number of bytes written, if this number does not match the requested number of bytes to be written, an error has occured. The return value of EAF_Read also indicates the number of bytes actually transmitted.