Enddo Integer Unit Error Stop Close Write Open File Program
Contents |
- especially when large amounts of data are involved. Too much keyboard input during the run of a program leads fortran open file to mistakes and tedium, while too much screen output has similar consequences. Putting
Fortran Read File
data into files - both for input and output - is a more leisurely and less error-prone approach. OpenThe open command
Fortran Runtime Error: End Of File
is used to open files - that is, it makes files available so that Fortran can read or write to them. The simplest form of the command is open (unit = number, file
Fortran Write To File
= "name") . In place of number you insert a positive integer (but not 6) to be used to refer to the file, and instead of name you insert the name of the file. Here are examples of open commands: open (unit = 2, file = "scores") open (unit = 7, file = "a:scores.txt") open (unit = 5, file = "h:\\results\\primes") open (unit = 24, file = fortran write statement "c:\\fortran\\data\\divisors.dat"). Fortran uses the unit number to access the file with later read and write statements. Several files can be open at once, but each must have a different number. There is one thing to remember about numbering a file - you cannot use the number 6, as GNU Fortran reserves that number to refer to the screen. Note that quotes enclose the filename. Also, in specifying a directory path for a file, you must use double backslashes instead of single ones. Do not put a space on either side of the colon after the drive letter. If you do not specify a drive or directory path for a file, or if you specify the same drive upon which GNU Fortran is installed but without a path, GNU Fortran will by default assume the file is located on the same drive and in the same directory from where Fortran is running. If the named file does not already exist, Fortran will create it; if it does exist, Fortran will replace it. (So don't mistakenly give the file the same name as another important file!) CloseThe close command is used to close one or more files - examples are close (5)
compiler. The first set of examples are for the Fortran II, IV, and 77 compilers. The remaining examples can be compiled and run with any newer standard Fortran compiler (see the end of the main Fortran fortran 90 open file article for lists of compilers). By convention most contemporary Fortran compilers select the language fortran 90 read file standard to use during compilation based on source code file name suffix: FORTRAN 77 for .f (or the less common .for), fortran write to screen Fortran 90 for .f90, Fortran 95 for .f95. Other standards, if supported, may be selected manually with a command line option. Contents 1 FORTRAN II, IV, and 77 compilers 1.1 Area Of a Triangle program http://www.math.hawaii.edu/~hile/fortran/fort7.htm 1.1.1 Simple Fortran II program 1.1.2 Simple Fortran IV program 1.1.3 Simple Fortran 77 program 2 "Retro" FORTRAN IV 2.1 Hello, World program 2.1.1 FORTRAN 66 (also FORTRAN IV) 2.1.2 FORTRAN 77 2.1.3 Fortran 90 3 Fortran 77 examples 3.1 Greatest common divisor 3.2 Complex numbers 4 Fortran 90/95 examples 4.1 Summations with a DO loop 4.2 Calculating cylinder area 4.3 Dynamic memory allocation and arrays 4.4 Writing functions 4.5 https://en.wikibooks.org/wiki/Fortran/Fortran_examples Writing subroutines 4.6 Internal and Elemental Procedures 4.7 Pointers and targets methods 4.8 Module programming FORTRAN II, IV, and 77 compilers[edit] NOTE: Before FORTRAN 90, most FORTRAN compilers enforced fixed-format source code, a carryover from IBM punch cards comments must begin with a * or C or ! in column 1 statement labels must occur in columns 1-5 continuation lines must have a non-blank character in column 6 statements must start in column 7 the line-length may be limited to 72 characters (derived from the 80-byte width of a punch-card, with last 8 characters reserved for (optional) sequence numbers) If errors are produced when you compile your FORTRAN code, first check the column alignment. Some compilers also offer free form source by using a compiler flag Area Of a Triangle program[edit] Simple Fortran II program[edit] One data card input If one of the input values is zero, then the program will end with an error code of "1" in the job control card listing following the execution of the program. Normal output will be one line printed with A, B, C, and AREA. No specific units are stated. C AREA OF A TRIANGLE - HERON'S FORMULA C INPUT - CARD READER UNIT 5, INTEGER INPUT C OUTPUT - L
disk, floppy) or a tape. In Fortran each file is associated with a unit number, an integer between 1 and 99. Some unit numbers are http://web.stanford.edu/class/me200c/tutorial_77/15_files.html reserved: 5 is standard input, 6 is standard output. Opening and closing a file Before you can use a file you have to open it. The command is open (list-of-specifiers) where the most common specifiers are: [UNIT=] u IOSTAT= ios ERR= err FILE= fname STATUS= sta ACCESS= acc FORM= frm RECL= rl The unit number u is a number in the range 1-99 that denotes this file (the open file programmer may chose any number but he/she has to make sure it is unique). ios is the I/O status identifier and should be an integer variable. Upon return, ios is zero if the statement was successful and returns a non-zero value otherwise. err is a label which the program will jump to if there is an error. fname is a character string denoting the file name. sta is fortran write to a character string that has to be either NEW, OLD or SCRATCH. It shows the prior status of the file. A scratch file is a file that is created when opened and deleted when closed (or the program ends). acc must be either SEQUENTIAL or DIRECT. The default is SEQUENTIAL. frm must be either FORMATTED or UNFORMATTED. The default is UNFORMATTED. rl specifies the length of each record in a direct-access file. For more details on these specifiers, see a good Fortran 77 book. After a file has been opened, you can access it by read and write statements. When you are done with the file, it should be closed by the statement close ([UNIT=]u[,IOSTAT=ios,ERR=err,STATUS=sta]) where, as usual, the parameters in brackets are optional. In this case sta is a character string which can be KEEP (the default) or DELETE. Read and write revisited The only necessary change from our previous simplified read/write statements, is that the unit number must be specified. But frequently one wants to add more specifiers. Here is how: read ([UNIT=]u, [FMT=]fmt, IOSTAT=ios, ERR=err, END=s) write([UNIT=]u, [FMT=]fmt, IOSTAT=ios, ERR=err, END=s) where most of the specifiers have been described above. The END=s specifier defines which statement label the program