Writing Data

Base.writeFunction
write(f::FortranFile, items...)
write(f::FortranFile, rec=N, items...)

Write a data record to a FortranFile. Each item should be a scalar of a Fortran-compatible datatype (e.g. Int32, Float64, FString{10}), or an array of such scalars. If no items are given, an empty record is written. Returns the number of bytes written, not including the space taken up by the record markers.

For direct-access files, the number of the record to be written must be specified with the rec keyword (N=1 for the first record).

source

Examples

The following examples show how to write Julia code that corresponds to certain Fortran WRITE statements. The Julia code assumes that f refers to an opened FortranFile in sequential access mode, while the Fortran code assumes that lun refers to a logical unit number for a connected file.

For direct access mode, each write call additionally needs to specify the number of the record to write, by using the rec keyword argument. E.g. to write the first record, use write(f, rec=1, ...).

Writing scalars

i = Int32(1)
write(f, i)

corresponds to

integer(kind=int32)::i
i = 1
write(lun) i

See Datatypes for the Julia equivalents of the Fortran datatypes.

Writing arrays

A = zeros(Float32, 10, 20)
write(f, A)

corresponds to

real(kind=real32),dimension(10,20)::A
A = 0.0
write(lun) A                          ! modern Fortran
write(lun) ((A(i,j), i=1,10), j=1,20) ! Fortran77

Writing strings

s = FString(20, "blabla")
write(f, s)

corresponds to

character(len=20)::s
s = "blabla"
write(lun) s

Writing a record with multiple data

Combining the above into a single record,

i = Int32(1)
A = zeros(Float32, 10, 20)
s = FString(20, "blabla")
write(f, i, A, s)

corresponds to

integer(kind=int32)::i
real(kind=real32),dimension(10,20)::A
character(len=20)::s
i = 1
A = 0.0
s = "blabla"
write(lun) i,A,s