12: File Operations
- Page ID
- 54221
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)
File operations allow Fortran programs to read from files and/or write to files. The basic read and write statements for file operations are the same as previously used with some additional information or clauses.
- 12.1: File Open
- A file must be opened before information can be written or read from a file. In order to open a file, the operating system needs some information about the file in order to correctly identify the file and establish the access parameters (i.e., read, write, etc.).
- 12.2: File Write
- The file must be opened for “write” or “readwrite” access before any information can be written to the file.
- 12.3: Stop Statement
- The Fortran stop statement will immediately terminate the program.
- 12.4: File Read
- The file must be opened for “read” or “readwrite” access before any information can be read from the file.
- 12.5: Rewind
- The rewind statement will reset the file read pointer and subsequent reads will start back at the beginning.
- 12.6: Backspace
- It is possible to read a line and then backspace and re-read the line again with the backspace statement.
- 12.7: Close File
- An open file should be closed when it is no longer needed.
- 12.8: Example
- In this example, we will write a Fortran program to read an input file and write a line number and the original line to an output file.