Files

Contents
Open with newunit
Read records
Write records
Close and status

Open with newunit

Open connects a variable to a file: newunit hands you a free unit number, file names the path, and status replace starts fresh.

Action write locks in your intent, so a later accidental read fails loudly instead of corrupting data.

program openfile implicit none integer :: u open(newunit=u, file="nums.txt", status="replace", action="write") write(u, *) 42 close(u) end program openfile

Read records

Reading mirrors writing: open with status old and action read, pull values with a matching read list, then close.

Status old demands that the file exists, which catches typos in the name before any data moves.

program readrec implicit none integer :: u integer :: n open(newunit=u, file="nums.txt", status="old", action="read") read(u, *) n close(u) print *, n end program readrec

./readrec

42

Write records

Formatted writes control each record: one write statement produces one line, and the format sets its look.

Here a loop writes 1, 2, and 3 on separate lines, each in a field three wide.

program writerec implicit none integer :: u integer :: i open(newunit=u, file="count.txt", status="replace", action="write") do i = 1, 3 write(u, "(I3)") i end do close(u) end program writerec

cat count.txt

1
2
3

Close and status

Close releases the unit, and its status decides the fate of the file: keep saves it while delete removes a scratch file at once.

Always close what you open: open units block other access, and unwritten buffers may never reach the disk.

program closestat implicit none integer :: u open(newunit=u, file="temp.txt", status="replace", action="write") write(u, *) "done" close(u, status="keep") end program closestat

Next: Subroutines and functions

Article author: Arthur Isaev

Related articles
Fortran
Fortran course: zero to hero in 50 lessons
What is Fortran and where it runs
Install gfortran
Your first program
Program structure
Types: integer real complex logical character
Kind parameters
Operators
Conditions
DO loops
Arrays
Array sections
Character strings
Formatted IO
Files
Subroutines and functions
Modules
USE with ONLY
INTENT
OPTIONAL and PRESENT
Explicit interfaces
Derived types
Type-bound procedures
Object orientation
Operator overloading
Finalization
Allocatable arrays
Pointers in Fortran
DO CONCURRENT
Coarrays
OpenMP basics
Namelists
IEEE arithmetic
Intrinsic matrix math
Random numbers
String tools
Command-line arguments
Environment variables
error stop
Testing
Build with gfortran and make
Debugging
C interoperability
From fixed form to free form
Numeric models
Performance
Modern Fortran tour: 2008, 2018, 2023
Capstone: stats tool
Capstone: 1D heat equation
Style guide
Hero roadmap: the whole course on one page
Install gfortran on Windows 11
Install gfortran on Ubuntu
Install gfortran on Rocky Linux
Install gfortran on macOS
Install gfortran on FreeBSD
Fortran in Docker
Banner image

Search this site

Channel @aofeed Chat @aofeedchat

Contacts and cooperation:
I recommend our hosting beget.com
Write to info@aredel.com if you:
1. Want to write an article for our site or translate an article into your native language.
2. Want to place thematically relevant ads on the site.
3. Ads on my site pass maximum censorship. If you see an ad block unsuitable for school-age children, shocking or misleading - please contact us by e-mail
4. Found a mistake, inaccuracy, bug, etc. on the site. ... .......
5. Articles can be shared on social media by clicking a network icon: