[comp.lang.fortran] F77 i/o compatible with assembly i/o help?

martin@humu.UUCP (Douglas W. Martin) (12/18/86)

     Can soneone help me figure out how to write unformatted,
sequential files in F77, such that they do not contain header info?
In PDP-11 Fortran, there existed functions iread and iwrite, with matching
read and write functions in assembly language.  It was thus quite easy to
write a file as output from an assembly language program, and later
read that file as input to a Fortran program.  How can this be done in F77?
Thanks in advance,
Doug Martin
NOSC-Hawaii Laboratories

UUCPmail: {akgua,allegra,decvax,ihnp4,ucbvax}!sdcsvax!noscvax!martin
MILNET:   martin@NOSC

pjw@alice.UUCP (12/22/86)

fortran unformatted output has to contain some kind of record boundaries
so BACKSPACE works.  The most likely way to write a binary file from
some other language and read it using fortran is to write fixed length records
and use f77 direct i/o.  (fortran's not a language that cooperates well
with anything else)

jerry@opal.berkeley.edu (Jerry Berkman) (12/23/86)

In article <407@humu.UUCP> martin@humu.UUCP (Douglas W. Martin) writes:
>
>
>     Can soneone help me figure out how to write unformatted,
>sequential files in F77, such that they do not contain header info?
>How can this be done in F77?
>Thanks in advance,
>Doug Martin
>NOSC-Hawaii Laboratories

4.x BSD VAX UNIX includes putc(ch) and fputc(lunit,ch) functions
which write a character to a logical unit with no headers, or new
lines.  There are corresponding getc() and fgetc() function.
See "man 3f putc" and "man 3f getc" for details.

	- Jerry Berkman
	  Academic Computing Services, U. C. Berkeley (415) 642-4804
	  jerry@ucbopal.Berkeley.EDU

rchrd@well.UUCP (Richard Friedman) (12/30/86)

Yuk!  I wouldnt want to call fputc for every character in
a megacharacter sequential file!  Better to call the C library
routines for writing binary files   fwrite   and    fread.
This assumes you can write an interface that works.

-- 

    ...Richard Friedman [rchrd]
       Pacific-Sierra Research
       2855 Telegraph #415, Berkeley CA 94705
       (415) 540 5216
     
       uucp:  {ucbvax,lll-lcc,ptsfa,hplabs}!well!rchrd
              - or -   rchrd@well.uucp

mink@cfa.harvard.EDU (Doug Mink) (12/31/86)

In article <407@humu.UUCP>, martin@humu.UUCP (Douglas W. Martin) writes:
 
>      Can soneone help me figure out how to write unformatted,
> sequential files in F77, such that they do not contain header info?
> In PDP-11 Fortran, there existed functions iread and iwrite, with matching
> read and write functions in assembly language.  It was thus quite easy to
> write a file as output from an assembly language program, and later
> read that file as input to a Fortran program.  How can this be done in F77?

I almost never use f77 file i/o anymore, and neither does anyone else I
know who does extensive f77 programming.  Each f77 programmer has their
own toolbox of useful C routines; here's part of the stream i/o portion
of mine.  The basic features of a f77-callable C subroutine are the
underscore after the subroutine name and the addition of string length
variables when a character string is passed.  Equivalent routines can of
course be written for {open(2),read(2),write(2),close(2)}.  Enjoy.

Doug Mink
mink@cfa.harvard.edu
{seismo!ihnp4}!harvard!cfa!mink

------------------------------------------------------------------------------

#include <sys/file.h>
#include </usr/include/stdio.h>

sfopen_ (idev,iflag,path,nc)

/*   Open UNIX file (Fortran-callable) for buffered I/O using system routines.
 *
 *   From F77:
 *
 *	Call SFOPEN (IDEV,IFLAG,PATH)
 *
 *	Integer*4 IDEV
 *			UNIX file descriptor (returned) (-1 if open failed)
 *	Integer*4 IFLAG
 *			0	open for reading
 *			1	create for writing
 *			2	create for reading and writing at start of file
 *			3	append: open for writing at end of file, or
 *				create for writing
 *	Character*(*) PATH
 *			Address of null-terminated file name (=path name)
 *
 *	nc	Number of characters in string PATH
 *		* this is not explicitly in the calling argument list;
 *		* Fortran adds string lengths to the end automatically
 */

char *path;
long *iflag;
long *idev;
int nc;

{
char *type;
FILE *stream;
long offset;

	if (*iflag == 0) type = "r";
	if (*iflag == 1) type = "w";
	if (*iflag == 2) type = "w+";
	if (*iflag == 3) type = "a";

	stream = fopen (path,type);

	if (stream == NULL)
		*idev = -1; /* set device to -1 if open failed*/
	else
		*idev = (long)stream;
	
	return;
}

sfput_ (idev,nbytes,buf,nwrit)

/*   Write to UNIX file (Fortran-callable) using buffered I/O.  
 *
 *   From F77:
 *
 *	Call SFPUT( IDEV,NBYTES,BUF,NWRIT)
 *
 *	Integer*4 IDEV
 *			UNIX file descriptor (input, returned from SFOPEN)
 *	Integer*4 NBYTES
 *			Number of bytes to write (input)
 *	(Integer,Real) BUF
 *			Address of buffer array (values written to file)
 *	Integer*4 NREAD
 *			Number of bytes written (returned, should=nbytes)
 */

char *buf;
long *nbytes,*nwrit;
long *idev;

{
FILE *stream;
int nw,nitems,size;

	nitems = *nbytes ;
	size = 1;
	stream = (FILE*)*idev;
	nw = fwrite(buf,size,nitems,stream);
	*nwrit = nw;
	
	return;
}


sfget_ (idev,nbytes,buf,nread)

/*   Read from UNIX file (Fortran-callable) using buffered I/O.
 *
 *   From F77:
 *
 *	Call SFGET (IDEV,NBYTES,BUF,NREAD)
 *
 *	Integer*4 IDEV
 *			UNIX file descriptor (input, returned from SFOPEN)
 *	Integer*4 NBYTES
 *			Number of bytes to read (input)
 *	(Integer,Real) BUF
 *			Address of buffer array (values returned)
 *	Integer*4 NREAD
 *			Number of bytes read (returned, should=nbytes)
 */

char *buf;
long *nbytes,*nread;
long *idev;

{
FILE *stream;
int nr,nitems,size;

	nitems = *nbytes ;
	size = 1;
	stream = (FILE*)*idev;
	nr = fread(buf,size,nitems,stream);
	*nread = nr;
	
	return;
}


sfclose_ (idev,ier)

/*   Close UNIX file opened for buffered I/O (Fortran-callable)
 *
 *   From F77:
 *
 *	Call SFCLOSE (IDEV,IER)
 *
 *	Integer*4 IDEV
 *			UNIX file descriptor (input, returned by SFOPEN)
 *	Integer*4 IER
 *			Error flag (0 => OK) (returned)
 */

long *idev;
long *ier;

{
FILE *stream;
long ptr;

	stream = (FILE*)*idev;
	ptr=fclose(stream);
	*ier=ptr;
	
	return;
}