Write entire file: Difference between revisions

Line 78:
Earlier Fortran might have other predefined unit numbers, for a card punch, paper tape, lineprinter, and so on, according to the installation. For a disc file, there may be no facility to name the file within Fortran itself as in the example OPEN statement because there may be no OPEN statement. Fortran IV used a DEFINE FILE statement, for example, which specified the record length and number of records in the file. As constants. File naming would instead be done by commands to the operating system when the prog. is started, as with the DD statement of JCL (Job Control Language) of IBM mainframes, and this would have its own jargon for NEW, OLD, REPLACE and what to do when the step finishes: DISP=KEEP, perhaps.
 
The OPEN statement started becoming standard with F77, but a READ or WRITE statement for a file could mix FORMAT style I/O with unformatted I/O - or indeed do both at once. This could be achieved via the A''n'' format code, which essentially states "unformatted" because it takes the variable's bit pattern "as is". Thus, a typical 32-bit floating-point variable occupies four bytes, andso it could be read or written with format code <code>A4</code> and so on for other sizes. With declarations like <code>REAL*8 X</code>, Fortran programmers are usually well aware of storage sizes.
 
With F90, constraints were tightened - for our own good, supposedly. A file is opened with FORM="FORMATTED" by default, meaning that text is expected to be involved and all READ or WRITE statements must use the FORMAT facilities for that file. In the example, the * specifies free-format, which is not format free. Contrariwise, if the file is opened with FORM="UNFORMATTED" then the FORMAT facility must ''not'' be specified, as in the second line of the example.
Line 84:
There is no particular limit on how much can be written in one go since no fixed record length has been specified, and indeed the text rolled forth via a variable may contain whatever character codes are desired, including CR, LF - but you will have to know whether the record separator is CR, CRLF, LFCR or CR, if you expect the file to be read later as a series of separate records. The output from each WRITE will be followed by the record separator (whichever it is) in the character interpretation in force, usually ASCII these days.
 
If instead FORM="UNFORMATTED" were specified in the OPEN statement you can still write whatever you wish, but now each output will not be followed by a record terminator. It is usual for such files to have a defined record length and reading or writing more than a record can hold is deemed an error. Previously (as with Fortran IV) the output (or input) would continue into however many successive records were needed. This was convenient when writing a large array of integers or floating-point, or indeed any list of such items. This would be useful when a complex data structure had been devised, and a simple statement with a short ''list'' would write the lot to a disc file, or read it back later. This would be far less trouble than working through its many details. One could go further and use EQUIVALENCE to overlay the pieces upon a large single array (called say THELOT) so that one need merely use <code>WRITE(F) THELOT</code> and likewise for reading. However keeping the equivalences correct when changes are made is troublesome. Instead, the data structure's collection of items could be placed in a COMMON work area and a STASH subroutine would for its COMMON statement merely declare an array THELOT of a suitable size without any mention of EQUIVALENCE. A somewhat similar arrangement is provided in pl/i via the BASED facility, and the word EQUIVALENCE does not appear.
 
=={{header|J}}==
1,220

edits