Recent Changes - Search:

Basics

Languages

Tools

OS

Resources

PmWiki

pmwiki.org

edit SideBar

WritingToAFile

Writing to a file in Fortran involves 3 steps:

1 - Opening a file

Before putting content into a file, it must be open. A general opening statement would look like:

open( unit, file = 'filename', status = 'stat')

where unit is a number which will be used to reference this file later, filename is the name of the file and stat is the state of the file, either new (it does not exist yet), old (it does exist) or unknown.

2 - Writing to a file

write(unit, format) var1, var2, ...

where unit is the same as was defined when opening the file, format defines how to write the variables and var1, var2, are the variables to write on that line.

It is also possible to write multiple values from a vector or matrix by using an implicit loop:

write(unit, format) (vec(i), i = 1, ni)
write(unit, format) (mat(:,j), j=1,nj

3 - Closing the file

Once the writing is done, it is good practice to close the file with:

close(unit)

Edit - History - Print - Recent Changes - Search
Page last modified on February 14, 2018, at 05:25 PM