ENG 213.1 · Computer Programming for Engineers
Our notes
1 min readWhy files
Lab readings, design tables and results are often too long to type each time. Programs read input files and write result files that a spreadsheet or plotting tool can open.
Opening and closing
open(unit=10, file='readings.txt', status='old', action='read')
open(unit=20, file='results.txt', status='replace', action='write')
...
close(10)
close(20)
- unit is a number (avoid 5 and 6, which are often the keyboard and screen).
- status: 'old' (must exist), 'new' (must not exist), 'replace', 'unknown'.
Reading until the end of the file
integer :: ios, n
real :: t, p
n = 0
do
read(10, *, iostat=ios) t, p
if (ios /= 0) exit
n = n + 1
end do
iostat is 0 on success, negative at end of file, positive on an error. Checking it stops the program crashing at the end of the data.
Writing results
write(20, '(F8.2, 2X, F10.3)') t, p
A comma-separated file opens neatly in a spreadsheet:
write(20, '(F8.2, A, F10.3)') t, ',', p
Worked example: heat loss through walls
A file walls.txt has one wall per line: thermal conductivity k (W/m·K), area A (m²), temperature difference ΔT (K), thickness L (m). The program reads each line and writes Q = kAΔT/L.
Line "0.8 10.0 20.0 0.2" gives Q = 0.8 × 10 × 20 / 0.2 = 800 W.
Line "0.04 10.0 20.0 0.05" (insulation) gives Q = 0.04 × 10 × 20 / 0.05 = 160 W.
Good habits
- Put a header line in result files saying what each column is, with units.
- Check iostat on OPEN as well (a missing file).
- Close files when done.
Exams usually ask
- Write statements to open, read from and write to a file.
- Explain the use of iostat and end-of-file detection.
Common mistake
Opening an input file with status='replace', which wipes it. Use 'old' for files you read.
Written by the UniHive team in our own words. Spotted a mistake? Tell us on WhatsApp.
Read
-
Best pick Introduction to Python Programming, 14.1 Reading from files
(opens openstax.org in a new tab)
OpenStax, Introduction to Python Programming, 14.1 Reading from files, CC BY-NC-SA 4.0 · licence
-
Computer file
(opens en.wikipedia.org in a new tab)
Wikipedia, Computer file, CC BY-SA 4.0 · licence
-
Comma-separated values
(opens en.wikipedia.org in a new tab)
Wikipedia, Comma-separated values, CC BY-SA 4.0 · licence
-
OpenStax, Introduction to Python Programming (whole free textbook)
The whole book, free to read online or download as a PDF (the PDF is large: Wi-Fi).
(opens openstax.org in a new tab)
OpenStax, OpenStax, Introduction to Python Programming (whole free textbook), CC BY-NC-SA 4.0 · licence
Watch
-
FORTRAN: READ AND PRINT STATEMENT
28 min · ≈ 126 MB · about ₦71
(opens youtube.com in a new tab)
-
[ Fortran Tuto 6] Opening and Writing Text Files
11 min · ≈ 50 MB · about ₦28
(opens youtube.com in a new tab)
-
How to use Fortran to data read file if the data are in a Matrix
8 min · ≈ 36 MB · about ₦20
(opens youtube.com in a new tab)
-
Fortran Hacks : Read and Write data from and to Sequential and Direct Access Binary Data Files.
27 min · ≈ 122 MB · about ₦69
(opens youtube.com in a new tab)
-
gfortran text file read modify write2016 07 18
7 min · ≈ 32 MB · about ₦18
(opens youtube.com in a new tab)
We link to free material and never copy it. Every link was opened by our team before it went up; video data is a rough guide at 360p.