Most computer systems require users to organize their files into records and blocks which reflect the way data is stored on disk. For example, on UDelVM, the default record size is 80 bytes. If you attempt to write a sequential file with records longer than 80 bytes you will get an error message indicating you have written beyond the end of the record. In UNIX, there is no default record size and no similar requirement exists for sequential files on UNIX. Sequential files are the most typical type of file used in FORTRAN programs.
For random access files (also called "direct access" files), you do need to define the record length under both VS FORTRAN and UNIX FORTRAN. In both cases, this is done in the open statement by using the "recl" parameter. This record length information is needed by the system to locate individual records in the file.
On UDelVM, your files may have fixed-length records or variable-length records. These are the two common "record formats" you are used to on UDelVM. On UNIX disk systems, records are always variable-length, so a UNIX parameter equivalent to the IBM "recfm" is not used.
For random access files under VS FORTRAN, the default number of records is 50. To reduce or increase the number of records below or above 50, one must use the filedef parameter "xtent". Under UNIX, a random access file will contain as few or as many logical records as the program generates and no parameter equivalent to xtent is required.
The "disp mod" filedef parameter is used in VS FORTRAN to append records to a sequential file. Under UNIX FORTRAN one uses the open statement's "access" parameter (access='append').
The most straightforward solution is to use the "file" parameter to link an I/O unit with a file. For example, to associate the file "mydata" with unit 5, add the following statement to your FORTRAN program:
open(5,file='mydata')This can be done with any I/O unit number. However, since the statement is part of the source code, it cannot be changed at execution time. Several alternatives are next illustrated that give you execution-time flexibility.
prog.exe < in > out
character filename*30 . print *, 'Enter the name of your input file' read(5,'(a)')) filename open(10,file=filename,.......)
character*30 argv . . . m = iargc() i = 1 do while (i.le.m) call getarg(i,argv) open(unit=i,file=argv,....) i=i+1 end do . . .For example, if the executable file "myprog.exe" contains this program segment, then the command
myprog.exe one.dat two.dat three.datwill connect the files one.dat, two.dat and three.dat to I/O units 1, 2 and 3 at execution time.
CAUTION: This particular example works when the I/O unit numbers are 5 or lower.
ln -s myfile.dat fort.3This makes a "symbolic link" between your file "myfile.dat" and the default filename "fort.3" that UNIX FORTRAN creates for I/O unit 3. Thus, "fort.3" functions as an pointer to the real file "myfile.dat."
Once this "ln" command is issued, all subsequent I/O using unit 3 is associated with "myfile.dat". You can eliminate this link by erasing the symbolic link file. For example, type
rm fort.3
This alternative does not work for units 5 and 6.
As an example, the following program segment reads from unit 1 and writes to unit 2.
. . call ioinit(.true.,.false.,.false.,'fort.',.false.) . . read(1,*).... . write(2,10).... .
man ioinitfor more information about the routine.
Before running the executable code, type the following statements to associate the file "input.data" with I/O unit 1 and "output.data" with I/O unit 2. Note that this alternative uses the naming convention fort.01, fort.02, etc. rather than the convention fort.1, fort.2, etc. used in alternative 3.
setenv fort.01 input.dat setenv fort.02 output.datThe advantage of using environment variables over the symbolic link is that a link file is not created in your directory. The disadvantage is that you must add the call to the FORTRAN ioinit subprogram.
dimension x(1000),alpha(3), beta(3), gamma(3) print *, 'Enter the Run Number' read(5,*) nrun read(1,*) n,(x(i),i=1,n) call xcalc(x,n,alpha,beta,gamma) write(2,'(1x,i4,9(e12.5,3x))') nrun,alpha,beta,gamma open(3,recl=40,access='direct',form='unformatted') write(3,rec=nrun) alpha, beta, gamma endFiledefs:
filedef 1 disk input file filedef 2 disk output1 file (lrecl 140 disp mod filedef 3 disk output2 file (xtent 100
The same result can be accomplished in UNIX with the following program:
dimension x(1000),alpha(3), beta(3), gamma(3) character filename(3)*20 print *, 'Enter the Run Number' read(5,*) nrun print *, 'Enter the filenames for files 1,2 and 3' read(5,'(a)') (filename(i),i=1,3) open(1,file=filename(1)) open(2,file=filename(2),access='append')) open(3,file=filename(3),access='direct',recl=40, 1 form='unformatted') read(1,*) n,(x(i),i=1,n) call xcalc(x,n,alpha,beta,gamma) write(2,'(1x,i4,9(e12.5,3x))') nrun,alpha,beta,gamma write(3,rec=nrun) alpha, beta, gamma end
**************************************************************** * * * Correspondence to VS FORTRAN * * * **************************************************************** * * * VS FORTRAN UNIX * * ========== ==== * * * * * * Defining the Record Length for A Sequential File * * ================================================ * * * * filedef 3 disk (lrecl 200 Unnecessary under UNIX FORTRAN * * * * * * Defining the Record Format * * ========================== * * * * filedef 1 disk (recfm f Unnecessary under UNIX FORTRAN * * * * * * Append Mode * * ============ * * * * filedef 2 disk (disp mod Use a FORTRAN Open statement * * * * open(......,access='append',...) * * * * * * Number of Random Access Records * * =============================== * * * * filedef 3 disk (xtent 100 Unnecessary under UNIX FORTRAN * * * * * * Naming the Input/Output File * * ============================ * * * * filedef 2 disk input data a print *, 'Enter filename' * * read (5,'(a)')filename * * open(unit=2,file=filename,...) * * read(2,12)...... * * * * or * * * * Use the iargc and getarg * * subprograms to get filenames * * from the command line. See * * text above. * * * * or * * * * Issue a symbolic link prior * * to running the program: * * * * ln -s input.data fort.2 * * * * or * * * * Use environment variables. * * See the text above. * * * ****************************************************************