Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!usc!samsung!sol.ctr.columbia.edu!cica!iuvax!copper!bomgard From: bomgard@copper.ucs.indiana.edu (Tim Bomgardner) Newsgroups: comp.lang.fortran Subject: File Handling in Fortran 77 Message-ID: <56017@iuvax.cs.indiana.edu> Date: 31 Aug 90 18:52:23 GMT Sender: news@iuvax.cs.indiana.edu Organization: Indiana University, Bloomington IN. Lines: 94 References: Wow. I got a lot more responses to that post than I expected. I'm going to simply include the code here for anyone who wants it. In general, no, I don't know if it works on your particular machine, but it should. One writer claimed it would not work on VAX/VMS because a VMS record length (RECL) is 4. This is only partly true. RECL denotes record length in processor dependent units for _unformatted_ files (4 on the VAX), but in _characters_ for formatted files. I erroneously left FORM='FORMATTED' out of the list of open statement parameters because it doesn't matter on my system. This code was compiled using Ryan-McFarland Fortran on a 386 PC running DOS 3.3. As far as I can tell, it works perfectly. I uploaded it to a VAX/VMS system, where it worked as is with the following caveat. The file system (which insists on doing more for you than you may want or need) created the file with the "Fortran carriage control" attribute set. This made it impossible to TYPE the output file. Setting CARRIAGECONTROL='NONE' in the open statement cured this problem. In practical terms, this means the program is not portable. We can argue about whether this is a Fortran problem or a problem with VMS's file system philosophy. c====================================================================== c The following (stripped down) routines provide a simple character- c oriented file i/o facility modeled after C's getc, putc, etc. c They are intended for illustrative purposes only. They require c direct access to the file; if the operating system won't allow it, c that's not Fortran's fault. c====================================================================== c c Open a file in a specified mode. Only 'r' mode is implemented c here. Only one file at a time can be opened. No error checking is c performed. c integer function fopen(filename, mode) character filename*(*), mode*(*) common /cio/ fileptr integer fileptr lun = 99 open(unit=lun, file=filename, status='old', access='direct', & form='formatted', recl=1) fileptr = 0 fopen = lun return end c====================================================================== c c Position the file pointer. Only C's SEEK_SET (beginning of file) is c implemented. No error checking is performed. c integer function fseek(lun, offset, whence) integer lun, offset, whence common /cio/ fileptr integer fileptr fileptr = 0 fseek = 0 return end c====================================================================== c c Get a character from an open file. No error checking is performed. c integer function getc(lun) integer lun common /cio/ fileptr integer fileptr character*1 khar read(unit=lun, fmt=7, rec=fileptr) khar 7 format(a) fileptr = fileptr + 1 getc = ichar(khar) return end c====================================================================== c c Write a character to an open file. No error checking is performed. c integer function putc(khar, lun) character*1 khar integer lun common /cio/ fileptr integer fileptr write(unit=lun, fmt=7, rec=fileptr) khar 7 format(a) fileptr = fileptr + 1 putc = ichar(khar) return end